Refine search
Assert.notNull(dataSource, "No DataSource specified"); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) { conHolder.requested(); if (!conHolder.hasConnection()) { logger.debug("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(fetchConnection(dataSource)); return conHolder.getConnection(); Connection con = fetchConnection(dataSource); if (TransactionSynchronizationManager.isSynchronizationActive()) { try { holderToUse = new ConnectionHolder(con); holderToUse.setConnection(con); holderToUse.requested(); TransactionSynchronizationManager.registerSynchronization( new ConnectionSynchronization(holderToUse, dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != conHolder) { TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
txObject.getConnectionHolder().isSynchronizedWithTransaction()) { Connection newCon = obtainDataSource().getConnection(); if (logger.isDebugEnabled()) { logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction"); txObject.setConnectionHolder(new ConnectionHolder(newCon), true); txObject.getConnectionHolder().setSynchronizedWithTransaction(true); con = txObject.getConnectionHolder().getConnection(); txObject.getConnectionHolder().setTransactionActive(true); txObject.getConnectionHolder().setTimeoutInSeconds(timeout); TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());
public void setRollbackOnly() { getConnectionHolder().setRollbackOnly(); }
@Override public void suspend() { if (this.holderActive) { TransactionSynchronizationManager.unbindResource(this.dataSource); if (this.connectionHolder.hasConnection() && !this.connectionHolder.isOpen()) { // Release Connection on suspend if the application doesn't keep // a handle to it anymore. We will fetch a fresh Connection if the // application accesses the ConnectionHolder again after resume, // assuming that it will participate in the same transaction. releaseConnection(this.connectionHolder.getConnection(), this.dataSource); this.connectionHolder.setConnection(null); } } }
@Override public void afterCompletion(int status) { // If we haven't closed the Connection in beforeCompletion, // close it now. The holder might have been used for other // cleanup in the meantime, for example by a Hibernate Session. if (this.holderActive) { // The thread-bound ConnectionHolder might not be available anymore, // since afterCompletion might get called from a different thread. TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource); this.holderActive = false; if (this.connectionHolder.hasConnection()) { releaseConnection(this.connectionHolder.getConnection(), this.dataSource); // Reset the ConnectionHolder: It might remain bound to the thread. this.connectionHolder.setConnection(null); } } this.connectionHolder.reset(); } }
@Override public Integer getTimeout() throws SQLException { ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (holder != null && holder.hasTimeout()) { return holder.getTimeToLiveInSeconds(); } return null; } }
JpaTransactionObject txObject = (JpaTransactionObject) transaction; if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) { throw new IllegalTransactionStateException( "Pre-bound JDBC Connection found! JpaTransactionManager does not support " + ConnectionHandle conHandle = getJpaDialect().getJdbcConnection(em, definition.isReadOnly()); if (conHandle != null) { ConnectionHolder conHolder = new ConnectionHolder(conHandle); if (timeoutToUse != TransactionDefinition.TIMEOUT_DEFAULT) { conHolder.setTimeoutInSeconds(timeoutToUse); TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); txObject.setConnectionHolder(conHolder); TransactionSynchronizationManager.bindResource( obtainEntityManagerFactory(), txObject.getEntityManagerHolder());
protected void doBegin(Object transaction, TransactionDefinition definition) { if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) { throw new IllegalTransactionStateException( "Pre-bound JDBC Connection found - PersistenceBrokerTransactionManager does not support " + ConnectionHolder conHolder = new ConnectionHolder(con); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { conHolder.setTimeoutInSeconds(definition.getTimeout()); logger.debug("Exposing OJB transaction as JDBC transaction [" + conHolder.getConnection() + "]"); TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); txObject.setConnectionHolder(conHolder); TransactionSynchronizationManager.bindResource(getPbKey(), txObject.getPersistenceBrokerHolder());
tm.setTransactionSynchronization(DataSourceTransactionManager.SYNCHRONIZATION_NEVER); TransactionTemplate tt = new TransactionTemplate(tm); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive()); ConnectionHolder conHolder = new ConnectionHolder(con); conHolder.setTransactionActive(true); TransactionSynchronizationManager.bindResource(ds, conHolder); final RuntimeException ex = new RuntimeException("Application exception"); try {
txObject.getEntityManagerHolder().setTimeoutInSeconds(timeoutToUse); ConnectionHandle conHandle = getJpaDialect().getJdbcConnection(em, definition.isReadOnly()); if (conHandle != null) { ConnectionHolder conHolder = new ConnectionHolder(conHandle); if (timeoutToUse != TransactionDefinition.TIMEOUT_DEFAULT) { conHolder.setTimeoutInSeconds(timeoutToUse); logger.debug("Exposing JPA transaction as JDBC [" + conHandle + "]"); TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); txObject.setConnectionHolder(conHolder);
@Test public void testAddInvoicesWithinTransaction() throws Exception { given(callableStatement.execute()).willReturn(false); given(callableStatement.getUpdateCount()).willReturn(-1); given(callableStatement.getObject(3)).willReturn(4); given(connection.prepareCall("{call " + AddInvoice.SQL + "(?, ?, ?)}") ).willReturn(callableStatement); TransactionSynchronizationManager.bindResource(dataSource, new ConnectionHolder(connection)); try { testAddInvoice(1106, 3); verify(callableStatement).setObject(1, 1106, Types.INTEGER); verify(callableStatement).setObject(2, 3, Types.INTEGER); verify(callableStatement).registerOutParameter(3, Types.INTEGER); verify(connection, never()).close(); } finally { TransactionSynchronizationManager.unbindResource(dataSource); connection.close(); } }
ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (holder == null || !holder.isSynchronizedWithTransaction()) { TransactionSynchronizationManager.registerSynchronization(springTxnLister);
protected void doBegin(Object transaction, TransactionDefinition definition) { if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) { throw new IllegalTransactionStateException( "Pre-bound JDBC Connection found - JdoTransactionManager does not support " + ConnectionHandle conHandle = getJdoDialect().getJdbcConnection(pm, definition.isReadOnly()); if (conHandle != null) { ConnectionHolder conHolder = new ConnectionHolder(conHandle); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { conHolder.setTimeoutInSeconds(definition.getTimeout()); logger.debug("Exposing JDO transaction as JDBC transaction [" + conHolder.getConnectionHandle() + "]"); TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); txObject.setConnectionHolder(conHolder); TransactionSynchronizationManager.bindResource( getPersistenceManagerFactory(), txObject.getPersistenceManagerHolder());
TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory()); TransactionSynchronizationManager.unbindResource(getDataSource()); ConnectionHandle conHandle = txObject.getConnectionHolder().getConnectionHandle(); if (conHandle != null) { try {
TransactionSynchronizationManager.unbindResource(getJdoManager()); TransactionSynchronizationManager.unbindResource(getDataSource()); try { getCastorDialect().releaseJdbcConnection(txObject.getConnectionHolder().getConnectionHandle(), txObject.getDatabaseHolder().getDatabase());
public Object getResource(Object key) { Object res = TransactionSynchronizationManager.getResource(key); if (res != null) { if (!(res instanceof ConnectionHolder)) { if (res instanceof JmsResourceHolder) { return ((JmsResourceHolder)res).getConnection(); } } else { return ((ConnectionHolder)res).getConnection(); } } return res; }
private void clearTransactionInfo() { ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(this); if (conHolder != null && conHolder. isSynchronizedWithTransaction()) { TransactionSynchronizationManager.unbindResourceIfPossible(this); } }
@Override protected void doCleanupAfterCompletion(Object transaction) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction; // Remove the connection holder from the thread, if exposed. if (txObject.isNewConnectionHolder()) { TransactionSynchronizationManager.unbindResource(obtainDataSource()); } // Reset connection. Connection con = txObject.getConnectionHolder().getConnection(); try { if (txObject.isMustRestoreAutoCommit()) { con.setAutoCommit(true); } DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel()); } catch (Throwable ex) { logger.debug("Could not reset JDBC Connection after transaction", ex); } if (txObject.isNewConnectionHolder()) { if (logger.isDebugEnabled()) { logger.debug("Releasing JDBC Connection [" + con + "] after transaction"); } DataSourceUtils.releaseConnection(con, this.dataSource); } txObject.getConnectionHolder().clear(); }
@Override protected Connection getConnectionFromTransactionManager() { try { Connection result = transactionManager.getDataSource().getConnection(); TransactionSynchronizationManager.bindResource(transactionManager.getDataSource(), new ConnectionHolder(result)); return result; } catch (final SQLException ex) { throw new ShardingException("Could not open JDBC Connection before transaction", ex); } } }
@Override public void unbindResource() { ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(transactionManager.getDataSource()); DataSourceUtils.releaseConnection(holder.getConnection(), transactionManager.getDataSource()); }