@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { Connection con = entityManager.unwrap(Connection.class); return (con != null ? new SimpleConnectionHandle(con) : null); }
@Override public ConnectionHandle getJdbcConnection(EntityManager em, boolean readOnly) throws PersistenceException, SQLException { AbstractSession session = (AbstractSession) getSession(em); // The connection was already acquired eagerly in beginTransaction, // unless lazyDatabaseTransaction was set to true. Connection con = session.getAccessor().getConnection(); return (con != null ? new SimpleConnectionHandle(con) : null); }
@Test public void testTransactionCommitWithDataSource() throws SQLException { final DataSource ds = mock(DataSource.class); JdoDialect dialect = mock(JdoDialect.class); final Connection con = mock(Connection.class); ConnectionHandle conHandle = new SimpleConnectionHandle(con); given(pmf.getPersistenceManager()).willReturn(pm); given(pm.currentTransaction()).willReturn(tx); TransactionTemplate tt = new TransactionTemplate(); given(dialect.getJdbcConnection(pm, false)).willReturn(conHandle); JdoTransactionManager tm = new JdoTransactionManager(); tm.setPersistenceManagerFactory(pmf); tm.setDataSource(ds); tm.setJdoDialect(dialect); tt.setTransactionManager(tm); final List l = new ArrayList(); l.add("test"); assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf)); assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds)); PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true); return l; } }); assertTrue("Correct result list", result == l); assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf)); assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds)); verify(pm).close(); verify(dialect).beginTransaction(tx, tt); verify(dialect).releaseJdbcConnection(conHandle, pm); verify(dialect).cleanupTransaction(null); verify(tx).commit(); }
@Test public void testTransactionCommitWithAutoDetectedDataSource() throws SQLException { final DataSource ds = mock(DataSource.class); JdoDialect dialect = mock(JdoDialect.class); final Connection con = mock(Connection.class); ConnectionHandle conHandle = new SimpleConnectionHandle(con); given(pmf.getConnectionFactory()).willReturn(ds); given(pmf.getPersistenceManager()).willReturn(pm); given(pm.currentTransaction()).willReturn(tx); TransactionTemplate tt = new TransactionTemplate(); given(dialect.getJdbcConnection(pm, false)).willReturn(conHandle); JdoTransactionManager tm = new JdoTransactionManager(); tm.setPersistenceManagerFactory(pmf); tm.setJdoDialect(dialect); tm.afterPropertiesSet(); tt.setTransactionManager(tm); final List l = new ArrayList(); l.add("test"); assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf)); assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds)); PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true); return l; } }); assertTrue("Correct result list", result == l); assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf)); assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds)); verify(pm).close(); verify(dialect).beginTransaction(tx, tt); verify(dialect).releaseJdbcConnection(conHandle, pm); verify(dialect).cleanupTransaction(null); verify(tx).commit(); }