org.springframework.jdbc.datasource.ConnectionHandle Java Examples
The following examples show how to use
org.springframework.jdbc.datasource.ConnectionHandle.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultJpaDialect.java From java-technology-stack with MIT License | 5 votes |
/** * This implementation always returns {@code null}, * indicating that no JDBC Connection can be provided. */ @Override @Nullable public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return null; }
Example #2
Source File: EclipseLinkJpaDialect.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { // As of Spring 4.1.2, we're using a custom ConnectionHandle for lazy retrieval // of the underlying Connection (allowing for deferred internal transaction begin // within the EclipseLink EntityManager) return new EclipseLinkConnectionHandle(entityManager); }
Example #3
Source File: HibernateJpaDialect.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { Session session = getSession(entityManager); return new HibernateConnectionHandle(session); }
Example #4
Source File: DefaultJpaDialect.java From lams with GNU General Public License v2.0 | 5 votes |
/** * This implementation always returns {@code null}, * indicating that no JDBC Connection can be provided. */ @Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return null; }
Example #5
Source File: DefaultJpaDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * This implementation always returns {@code null}, * indicating that no JDBC Connection can be provided. */ @Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return null; }
Example #6
Source File: HibernateJpaDialect.java From java-technology-stack with MIT License | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { Session session = getSession(entityManager); return new HibernateConnectionHandle(session); }
Example #7
Source File: EclipseLinkJpaDialect.java From java-technology-stack with MIT License | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { // As of Spring 4.1.2, we're using a custom ConnectionHandle for lazy retrieval // of the underlying Connection (allowing for deferred internal transaction begin // within the EclipseLink EntityManager) return new EclipseLinkConnectionHandle(entityManager); }
Example #8
Source File: EclipseLinkJpaDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { // As of Spring 4.1.2, we're using a custom ConnectionHandle for lazy retrieval // of the underlying Connection (allowing for deferred internal transaction begin // within the EclipseLink EntityManager) return new EclipseLinkConnectionHandle(entityManager); }
Example #9
Source File: HibernateJpaDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { Session session = getSession(entityManager); return new HibernateConnectionHandle(session); }
Example #10
Source File: HibernateJpaDialect.java From spring-analysis-note with MIT License | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { Session session = getSession(entityManager); return new HibernateConnectionHandle(session); }
Example #11
Source File: EclipseLinkJpaDialect.java From spring-analysis-note with MIT License | 5 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { // As of Spring 4.1.2, we're using a custom ConnectionHandle for lazy retrieval // of the underlying Connection (allowing for deferred internal transaction begin // within the EclipseLink EntityManager) return new EclipseLinkConnectionHandle(entityManager); }
Example #12
Source File: DefaultJpaDialect.java From spring-analysis-note with MIT License | 5 votes |
/** * This implementation always returns {@code null}, * indicating that no JDBC Connection can be provided. */ @Override @Nullable public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return null; }
Example #13
Source File: OpenJpaDialect.java From syncope with Apache License 2.0 | 4 votes |
@Override public ConnectionHandle getJdbcConnection(final EntityManager entityManager, final boolean readOnly) throws PersistenceException, SQLException { return new OpenJpaConnectionHandle(getOpenJPAEntityManager(entityManager)); }
Example #14
Source File: JdoTransactionManagerTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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(); }
Example #15
Source File: JdoTransactionManagerTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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(); }
Example #16
Source File: OpenJpaDialect.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return new OpenJpaConnectionHandle(getOpenJPAEntityManager(entityManager)); }
Example #17
Source File: JpaTransactionManager.java From spring-analysis-note with MIT License | 4 votes |
@Override protected void doCleanupAfterCompletion(Object transaction) { JpaTransactionObject txObject = (JpaTransactionObject) transaction; // Remove the entity manager holder from the thread, if still there. // (Could have been removed by EntityManagerFactoryUtils in order // to replace it with an unsynchronized EntityManager). if (txObject.isNewEntityManagerHolder()) { TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory()); } txObject.getEntityManagerHolder().clear(); // Remove the JDBC connection holder from the thread, if exposed. if (getDataSource() != null && txObject.hasConnectionHolder()) { TransactionSynchronizationManager.unbindResource(getDataSource()); ConnectionHandle conHandle = txObject.getConnectionHolder().getConnectionHandle(); if (conHandle != null) { try { getJpaDialect().releaseJdbcConnection(conHandle, txObject.getEntityManagerHolder().getEntityManager()); } catch (Exception ex) { // Just log it, to keep a transaction-related exception. logger.error("Could not close JDBC connection after transaction", ex); } } } getJpaDialect().cleanupTransaction(txObject.getTransactionData()); // Remove the entity manager holder from the thread. if (txObject.isNewEntityManagerHolder()) { EntityManager em = txObject.getEntityManagerHolder().getEntityManager(); if (logger.isDebugEnabled()) { logger.debug("Closing JPA EntityManager [" + em + "] after transaction"); } EntityManagerFactoryUtils.closeEntityManager(em); } else { logger.debug("Not closing pre-bound JPA EntityManager after transaction"); } }
Example #18
Source File: OpenJpaDialect.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException { return new OpenJpaConnectionHandle(getOpenJPAEntityManager(entityManager)); }
Example #19
Source File: JpaTransactionManager.java From java-technology-stack with MIT License | 4 votes |
@Override protected void doCleanupAfterCompletion(Object transaction) { JpaTransactionObject txObject = (JpaTransactionObject) transaction; // Remove the entity manager holder from the thread, if still there. // (Could have been removed by EntityManagerFactoryUtils in order // to replace it with an unsynchronized EntityManager). if (txObject.isNewEntityManagerHolder()) { TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory()); } txObject.getEntityManagerHolder().clear(); // Remove the JDBC connection holder from the thread, if exposed. if (getDataSource() != null && txObject.hasConnectionHolder()) { TransactionSynchronizationManager.unbindResource(getDataSource()); ConnectionHandle conHandle = txObject.getConnectionHolder().getConnectionHandle(); if (conHandle != null) { try { getJpaDialect().releaseJdbcConnection(conHandle, txObject.getEntityManagerHolder().getEntityManager()); } catch (Exception ex) { // Just log it, to keep a transaction-related exception. logger.error("Could not close JDBC connection after transaction", ex); } } } getJpaDialect().cleanupTransaction(txObject.getTransactionData()); // Remove the entity manager holder from the thread. if (txObject.isNewEntityManagerHolder()) { EntityManager em = txObject.getEntityManagerHolder().getEntityManager(); if (logger.isDebugEnabled()) { logger.debug("Closing JPA EntityManager [" + em + "] after transaction"); } EntityManagerFactoryUtils.closeEntityManager(em); } else { logger.debug("Not closing pre-bound JPA EntityManager after transaction"); } }
Example #20
Source File: DefaultJdoDialect.java From lams with GNU General Public License v2.0 | 3 votes |
/** * This implementation returns a DataStoreConnectionHandle for JDO. * <p><b>NOTE:</b> A JDO DataStoreConnection is always a wrapper, * never the native JDBC Connection. If you need access to the native JDBC * Connection (or the connection pool handle, to be unwrapped via a Spring * NativeJdbcExtractor), override this method to return the native * Connection through the corresponding vendor-specific mechanism. * <p>A JDO DataStoreConnection is only "borrowed" from the PersistenceManager: * it needs to be returned as early as possible. Effectively, JDO requires the * fetched Connection to be closed before continuing PersistenceManager work. * For this reason, the exposed ConnectionHandle eagerly releases its JDBC * Connection at the end of each JDBC data access operation (that is, on * {@code DataSourceUtils.releaseConnection}). * @see javax.jdo.PersistenceManager#getDataStoreConnection() * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ @Override public ConnectionHandle getJdbcConnection(PersistenceManager pm, boolean readOnly) throws JDOException, SQLException { return new DataStoreConnectionHandle(pm); }
Example #21
Source File: DefaultJdoDialect.java From spring4-understanding with Apache License 2.0 | 3 votes |
/** * This implementation returns a DataStoreConnectionHandle for JDO. * <p><b>NOTE:</b> A JDO DataStoreConnection is always a wrapper, * never the native JDBC Connection. If you need access to the native JDBC * Connection (or the connection pool handle, to be unwrapped via a Spring * NativeJdbcExtractor), override this method to return the native * Connection through the corresponding vendor-specific mechanism. * <p>A JDO DataStoreConnection is only "borrowed" from the PersistenceManager: * it needs to be returned as early as possible. Effectively, JDO requires the * fetched Connection to be closed before continuing PersistenceManager work. * For this reason, the exposed ConnectionHandle eagerly releases its JDBC * Connection at the end of each JDBC data access operation (that is, on * {@code DataSourceUtils.releaseConnection}). * @see javax.jdo.PersistenceManager#getDataStoreConnection() * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ @Override public ConnectionHandle getJdbcConnection(PersistenceManager pm, boolean readOnly) throws JDOException, SQLException { return new DataStoreConnectionHandle(pm); }
Example #22
Source File: DefaultJpaDialect.java From java-technology-stack with MIT License | 2 votes |
/** * This implementation does nothing, assuming that the Connection * will implicitly be closed with the EntityManager. * <p>If the JPA implementation returns a Connection handle that it expects * the application to close after use, the dialect implementation needs to invoke * {@code Connection.close()} (or some other method with similar effect) here. * @see java.sql.Connection#close() */ @Override public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em) throws PersistenceException, SQLException { }
Example #23
Source File: DefaultJpaDialect.java From spring-analysis-note with MIT License | 2 votes |
/** * This implementation does nothing, assuming that the Connection * will implicitly be closed with the EntityManager. * <p>If the JPA implementation returns a Connection handle that it expects * the application to close after use, the dialect implementation needs to invoke * {@code Connection.close()} (or some other method with similar effect) here. * @see java.sql.Connection#close() */ @Override public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em) throws PersistenceException, SQLException { }
Example #24
Source File: JpaDialect.java From spring-analysis-note with MIT License | 2 votes |
/** * Retrieve the JDBC Connection that the given JPA EntityManager uses underneath, * if accessing a relational database. This method will just get invoked if actually * needing access to the underlying JDBC Connection, usually within an active JPA * transaction (for example, by JpaTransactionManager). The returned handle will * be passed into the {@code releaseJdbcConnection} method when not needed anymore. * <p>This strategy is necessary as JPA does not provide a standard way to retrieve * the underlying JDBC Connection (due to the fact that a JPA implementation might not * work with a relational database at all). * <p>Implementations are encouraged to return an unwrapped Connection object, i.e. * the Connection as they got it from the connection pool. This makes it easier for * application code to get at the underlying native JDBC Connection, like an * OracleConnection, which is sometimes necessary for LOB handling etc. We assume * that calling code knows how to properly handle the returned Connection object. * <p>In a simple case where the returned Connection will be auto-closed with the * EntityManager or can be released via the Connection object itself, an * implementation can return a SimpleConnectionHandle that just contains the * Connection. If some other object is needed in {@code releaseJdbcConnection}, * an implementation should use a special handle that references that other object. * @param entityManager the current JPA EntityManager * @param readOnly whether the Connection is only needed for read-only purposes * @return a handle for the Connection, to be passed into {@code releaseJdbcConnection}, * or {@code null} if no JDBC Connection can be retrieved * @throws javax.persistence.PersistenceException if thrown by JPA methods * @throws java.sql.SQLException if thrown by JDBC methods * @see #releaseJdbcConnection * @see org.springframework.jdbc.datasource.ConnectionHandle#getConnection * @see org.springframework.jdbc.datasource.SimpleConnectionHandle * @see JpaTransactionManager#setDataSource */ @Nullable ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException;
Example #25
Source File: JpaDialect.java From spring-analysis-note with MIT License | 2 votes |
/** * Release the given JDBC Connection, which has originally been retrieved * via {@code getJdbcConnection}. This should be invoked in any case, * to allow for proper release of the retrieved Connection handle. * <p>An implementation might simply do nothing, if the Connection returned * by {@code getJdbcConnection} will be implicitly closed when the JPA * transaction completes or when the EntityManager is closed. * @param conHandle the JDBC Connection handle to release * @param entityManager the current JPA EntityManager * @throws javax.persistence.PersistenceException if thrown by JPA methods * @throws java.sql.SQLException if thrown by JDBC methods * @see #getJdbcConnection */ void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager entityManager) throws PersistenceException, SQLException;
Example #26
Source File: JdoDialect.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * Release the given JDBC Connection, which has originally been retrieved * via {@code getJdbcConnection}. This should be invoked in any case, * to allow for proper release of the retrieved Connection handle. * <p>An implementation might simply do nothing, if the Connection returned * by {@code getJdbcConnection} will be implicitly closed when the JDO * transaction completes or when the PersistenceManager is closed. * @param conHandle the JDBC Connection handle to release * @param pm the current JDO PersistenceManager * @throws JDOException if thrown by JDO methods * @throws SQLException if thrown by JDBC methods * @see #getJdbcConnection */ void releaseJdbcConnection(ConnectionHandle conHandle, PersistenceManager pm) throws JDOException, SQLException;
Example #27
Source File: JdoDialect.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * Retrieve the JDBC Connection that the given JDO PersistenceManager uses underneath, * if accessing a relational database. This method will just get invoked if actually * needing access to the underlying JDBC Connection, usually within an active JDO * transaction (for example, by JdoTransactionManager). The returned handle will * be passed into the {@code releaseJdbcConnection} method when not needed anymore. * <p>Implementations are encouraged to return an unwrapped Connection object, i.e. * the Connection as they got it from the connection pool. This makes it easier for * application code to get at the underlying native JDBC Connection, like an * OracleConnection, which is sometimes necessary for LOB handling etc. We assume * that calling code knows how to properly handle the returned Connection object. * <p>In a simple case where the returned Connection will be auto-closed with the * PersistenceManager or can be released via the Connection object itself, an * implementation can return a SimpleConnectionHandle that just contains the * Connection. If some other object is needed in {@code releaseJdbcConnection}, * an implementation should use a special handle that references that other object. * @param pm the current JDO PersistenceManager * @param readOnly whether the Connection is only needed for read-only purposes * @return a handle for the JDBC Connection, to be passed into * {@code releaseJdbcConnection}, or {@code null} * if no JDBC Connection can be retrieved * @throws JDOException if thrown by JDO methods * @throws SQLException if thrown by JDBC methods * @see #releaseJdbcConnection * @see org.springframework.jdbc.datasource.ConnectionHandle#getConnection * @see org.springframework.jdbc.datasource.SimpleConnectionHandle * @see JdoTransactionManager#setDataSource * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor */ ConnectionHandle getJdbcConnection(PersistenceManager pm, boolean readOnly) throws JDOException, SQLException;
Example #28
Source File: DefaultJdoDialect.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * This implementation does nothing, assuming that the Connection * will implicitly be closed with the PersistenceManager. * <p>If the JDO provider returns a Connection handle that it * expects the application to close, the dialect needs to invoke * {@code Connection.close} here. * @see java.sql.Connection#close() */ @Override public void releaseJdbcConnection(ConnectionHandle conHandle, PersistenceManager pm) throws JDOException, SQLException { }
Example #29
Source File: JpaDialect.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * Release the given JDBC Connection, which has originally been retrieved * via {@code getJdbcConnection}. This should be invoked in any case, * to allow for proper release of the retrieved Connection handle. * <p>An implementation might simply do nothing, if the Connection returned * by {@code getJdbcConnection} will be implicitly closed when the JPA * transaction completes or when the EntityManager is closed. * @param conHandle the JDBC Connection handle to release * @param entityManager the current JPA EntityManager * @throws javax.persistence.PersistenceException if thrown by JPA methods * @throws java.sql.SQLException if thrown by JDBC methods * @see #getJdbcConnection */ void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager entityManager) throws PersistenceException, SQLException;
Example #30
Source File: JpaDialect.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * Retrieve the JDBC Connection that the given JPA EntityManager uses underneath, * if accessing a relational database. This method will just get invoked if actually * needing access to the underlying JDBC Connection, usually within an active JPA * transaction (for example, by JpaTransactionManager). The returned handle will * be passed into the {@code releaseJdbcConnection} method when not needed anymore. * <p>This strategy is necessary as JPA does not provide a standard way to retrieve * the underlying JDBC Connection (due to the fact that a JPA implementation might not * work with a relational database at all). * <p>Implementations are encouraged to return an unwrapped Connection object, i.e. * the Connection as they got it from the connection pool. This makes it easier for * application code to get at the underlying native JDBC Connection, like an * OracleConnection, which is sometimes necessary for LOB handling etc. We assume * that calling code knows how to properly handle the returned Connection object. * <p>In a simple case where the returned Connection will be auto-closed with the * EntityManager or can be released via the Connection object itself, an * implementation can return a SimpleConnectionHandle that just contains the * Connection. If some other object is needed in {@code releaseJdbcConnection}, * an implementation should use a special handle that references that other object. * @param entityManager the current JPA EntityManager * @param readOnly whether the Connection is only needed for read-only purposes * @return a handle for the Connection, to be passed into {@code releaseJdbcConnection}, * or {@code null} if no JDBC Connection can be retrieved * @throws javax.persistence.PersistenceException if thrown by JPA methods * @throws java.sql.SQLException if thrown by JDBC methods * @see #releaseJdbcConnection * @see org.springframework.jdbc.datasource.ConnectionHandle#getConnection * @see org.springframework.jdbc.datasource.SimpleConnectionHandle * @see JpaTransactionManager#setDataSource * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor */ ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException;