混合事务
在ORM框架的事务管理器的事务内,使用JdbcTemplate执行SQL是不会纳入事务管理的。
下面进行源码分析,看为什么必须要在DataSourceTransactionManager的事务内使用JdbcTemplate。
1.开启事务
DataSourceTransactionManager
protected void doBegin(Object transaction,TransactionDefinition definition) { DataSourceTransactionObjecttxObject = (DataSourceTransactionObject) transaction; Connection con = null; try { if(txObject.getConnectionHolder() == null || txObject.getConnectionHolder().isSynchronizedWithTransaction()){ ConnectionnewCon = this.dataSource.getConnection(); if(logger.isDebugEnabled()) { logger.debug("AcquiredConnection [" + newCon + "] for JDBC transaction"); } txObject.setConnectionHolder(newConnectionHolder(newCon),true); } txObject.getConnectionHolder().setSynchronizedWithTransaction(true); con =txObject.getConnectionHolder().getConnection(); IntegerpreviousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con,definition); txObject.setPreviousIsolationLevel(previousIsolationLevel); // Switch to manualcommit if necessary. This is very expensive in some JDBC drivers,// so we don't wantto do it unnecessarily (for example if we've explicitly // configured theconnection pool to set it already). if(con.getAutoCommit()) { txObject.setMustRestoreAutoCommit(true); if(logger.isDebugEnabled()) { logger.debug("SwitchingJDBC Connection [" + con + "] to manual commit"); } con.setAutoCommit(false); } txObject.getConnectionHolder().setTransactionActive(true); int timeout =determineTimeout(definition); if (timeout !=TransactionDefinition.TIMEOUT_DEFAULT) { txObject.getConnectionHolder().setTimeoutInSeconds(timeout); } // Bind the sessionholder to the thread. if(txObject.isNewConnectionHolder()) { TransactionSynchronizationManager.bindResource(getDataSource(),txObject.getConnectionHolder()); } } catch (Exception ex) { DataSourceUtils.releaseConnection(con,this.dataSource); throw newCannotCreateTransactionException("Could not open JDBC Connection fortransaction",ex); } }
doBegin()方法会以数据源名为Key,ConnectionHolder(保存着连接)为Value,将已经开启事务的数据库连接绑定到一个ThreadLocal变量上。
2.绑定连接
public static void bindResource(Objectkey,Object value) throws IllegalStateException { Object actualKey =TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Assert.notNull(value,"Value must not be null"); Map<Object,Object> map = resources.get(); // set ThreadLocal Map ifnone found if (map == null) { map = newHashMap<Object,Object>(); resources.set(map); } Object oldValue = map.put(actualKey,value); // Transparently suppress aResourceHolder that was marked as void... if (oldValue instanceofResourceHolder && ((ResourceHolder) oldValue).isVoid()) { oldValue = null; } if (oldValue != null) { throw newIllegalStateException("Already value [" + oldValue + "] for key[" + actualKey+ "] bound to thread [" + Thread.currentThread().getName() +"]"); } if (logger.isTraceEnabled()){ logger.trace("Boundvalue [" + value + "] for key [" + actualKey + "] to thread[" + Thread.currentThread().getName()+ "]"); } }
resources变量就是上面提到的ThreadLocal变量,这样后续JdbcTemplate就可以用DataSource作为Key,查找到这个数据库连接。
3.执行SQL
JdbcTemplate
public Objectexecute(PreparedStatementCreator psc,PreparedStatementCallback action) throwsDataAccessException { Assert.notNull(psc,"PreparedStatementCreator must not be null"); Assert.notNull(action,"Callback object must not be null"); if (logger.isDebugEnabled()){ String sql =getSql(psc); logger.debug("Executingprepared SQL statement" + (sql != null ? " [" + sql +"]" : "")); } Connection con = DataSourceUtils.getConnection(getDataSource()); PreparedStatement ps = null; try { Connection conToUse= con; if(this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()){ conToUse =this.nativeJdbcExtractor.getNativeConnection(con); } ps =psc.createPreparedStatement(conToUse); applyStatementSettings(ps); PreparedStatementpsToUse = ps; if(this.nativeJdbcExtractor != null) { psToUse =this.nativeJdbcExtractor.getNativePreparedStatement(ps); } Object result =action.doInPreparedStatement(psToUse); handleWarnings(ps); return result; } catch (SQLException ex) { // ReleaseConnection early,to avoid potential connection pool deadlock // in the case whenthe exception translator hasn't been initialized yet. if (psc instanceofParameterDisposer) { ((ParameterDisposer)psc).cleanupParameters(); } String sql =getSql(psc); psc = null; JdbcUtils.closeStatement(ps); ps = null; DataSourceUtils.releaseConnection(con,getDataSource()); con = null; throwgetExceptionTranslator().translate("PreparedStatementCallback",sql,ex); } finally { if (psc instanceofParameterDisposer) { ((ParameterDisposer)psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.releaseConnection(con,getDataSource()); } }
4.获得连接
DataSourceUtils
public static Connection doGetConnection(DataSourcedataSource) throws SQLException { 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("Fetchingresumed JDBC Connection from DataSource"); conHolder.setConnection(dataSource.getConnection()); } returnconHolder.getConnection(); } // Else we either got noholder or an empty thread-bound holder here. logger.debug("FetchingJDBC Connection from DataSource"); Connection con =dataSource.getConnection(); if (TransactionSynchronizationManager.isSynchronizationActive()){ logger.debug("Registeringtransaction synchronization for JDBC Connection"); // Use sameConnection for further JDBC actions within the transaction. // Thread-boundobject will get removed by synchronization at transaction completion. ConnectionHolderholderToUse = conHolder; if (holderToUse ==null) { holderToUse= new ConnectionHolder(con); } else { holderToUse.setConnection(con); } holderToUse.requested(); TransactionSynchronizationManager.registerSynchronization( newConnectionSynchronization(holderToUse,dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse !=conHolder) { TransactionSynchronizationManager.bindResource(dataSource,holderToUse); } } return con; }