1 16 17 package org.springframework.orm.jpa.vendor; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import javax.persistence.EntityManager; 23 import javax.persistence.PersistenceException; 24 25 import oracle.toplink.essentials.internal.sessions.AbstractSession; 26 import oracle.toplink.essentials.sessions.Session; 27 import oracle.toplink.essentials.sessions.UnitOfWork; 28 29 import org.springframework.jdbc.datasource.ConnectionHandle; 30 import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; 31 import org.springframework.jdbc.datasource.SimpleConnectionHandle; 32 import org.springframework.orm.jpa.DefaultJpaDialect; 33 import org.springframework.transaction.TransactionDefinition; 34 import org.springframework.transaction.TransactionException; 35 36 55 public class TopLinkJpaDialect extends DefaultJpaDialect { 56 57 private boolean lazyDatabaseTransaction = false; 58 59 60 72 public void setLazyDatabaseTransaction(boolean lazyDatabaseTransaction) { 73 this.lazyDatabaseTransaction = lazyDatabaseTransaction; 74 } 75 76 77 @Override 78 public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) 79 throws PersistenceException, SQLException , TransactionException { 80 81 super.beginTransaction(entityManager, definition); 82 if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { 83 UnitOfWork uow = (UnitOfWork) getSession(entityManager); 87 uow.beginEarlyTransaction(); 88 } 89 return null; 91 } 92 93 @Override 94 public ConnectionHandle getJdbcConnection(EntityManager em, boolean readOnly) 95 throws PersistenceException, SQLException { 96 97 AbstractSession session = (AbstractSession) getSession(em); 98 Connection con = session.getAccessor().getConnection(); 101 return (con != null ? new SimpleConnectionHandle(con) : null); 102 } 103 104 107 protected Session getSession(EntityManager em) { 108 oracle.toplink.essentials.ejb.cmp3.EntityManager emi = (oracle.toplink.essentials.ejb.cmp3.EntityManager) em; 109 return emi.getActiveSession(); 110 } 111 112 } 113 | Popular Tags |