Spring是一个IOC框架,在此IOC框架的基础上,提供了DAO集成,AOP事务控制,JNDI等等一系列的高级功能,个人觉得,在Spring中最值得称道的不仅仅他是一个非入侵的IOC容器,而在于其神奇的声明事务以及异常处理;
为什么要使用事务,银行转账的例子都用烂了,这里就不再累赘,JDBC的本地事务利用Connention.setAutoCommit()的方法来保证的;
public Boolean updateAMoney(Conn){ ………………… } public Boolean updateAMoney(Conn){ ………………… }
分别对数据库做了两次更新
public Boolean MoneyAToB(){ conn.setAutoCommit(false); Try{ updateAMoney(conn); updateBMoney(conn); conn.commit(); }catch(){ conn.close(); } }
更新A操作出现异常,或者更新B异常,代码都无法到达conn.commit(),如此便此处的数据永远都不可能真正提交,无论A操作失败还是B操作失败,都不会对数据库产生影响;
我们在系统开发中,一般都会考虑系统架构分层,大致如下:
viewer------>control------->service-------->dao-------->infrastucture
viewer:显示数据和接受用户输入;
control:接受前端请求,进行逻辑处理,调用相应服务进行处理,将处理后的数据推往UI层进行展示;
service:通过接口提供系统服务;
dao:直接与数据库等基础设备进行交互,实现数据的CURD等操作;
infrastucture:基础设备;
在之前的实例中,我们可以将updateAMoney和updateBMoney看做Dao中的部分,而moneyAToB则因为具有业务含义,按照领域模型驱动设计中,他涉及到多个实体的更改,应该是被提至服务层的;
在Dao中,因为细粒度和不直接向外提供服务的缘由,我们在此层中不涉及事务操作,而将事务都推至服务层,但此时,我们可以发现,在Service中出现了connection,他不得不和JDBC耦合起来,出现了代码的坏味道,但是如果不出现Connection,我们又怎么保证updateAMoney和updateBMoney在同一个物理connection从而能够保证在一个事务中呢?
“一个请求,一个连接,一个服务,一个事务”,这是数据库事务设计的最佳模式;
要在Dao层中,要保证Dao中每一个Dao方法可能在同一个物理连接Connection中,则必须将Connection来自外部设置:
从Service中传入参数中获取;
从环境中获取
为了从Service中解耦,我们只能选择方式2,而一个请求,通常为一个线程,我们在本地环境中设置一个连接容器:
Public abstract ConnectionHolder{ Public static ThreadLoacl<Connection> connections=new NamedThreadLocal<Connection>(“….”) Public Connection getConnetion(){ Return connections.get(); }} Public Boolean commit(){ ……………………. } Public Boolean rollback(){……} }
我们在service中
Public Boolean moneyAToB(){ updateAMoney(); updateBMoney(); ConnectionHolder.commit(); } Public Boolean updateAMoney(){ Connection conn=connetionHolder.getConnction(); …………………………… }
这样,Service就可以不直接依赖Connection,但是仍然不能不依赖ConnnectionHolder,Spring的做法就是借助Aop,将这段代码搬到Xml文件或者annotation中来进行解耦,在方法结束也是invocationAction.invoke()后进行commit操作;
现在我们来看一个Spring的实现:
在Spring中,我们的Dao借助于扩展JDBCDaoSupport来实现的,他在配置的时候注入一个DataSource,这是一个连接池,我们可以在Dao方法中进行如下实现:
Public class CoreyDao extends JDBCDaoSupport{ Public Boolean updateAMoney(){ getJdbcTemplate() .execute(……….) } }
他将数据库操作委托给了jdbcTemplate,而jdbcTemplate他的数据库物理连接时如何而来的呢?源码如下:
Connection con = DataSourceUtils.getConnection(getDataSource());
他是借助于DataSourceUtils取得的,你可不要以为DataSourceUtils就是简单的从Datasource.getConnection从数据库连接池中获取连接,因为之前我们说过,从数据库中获取连接并不能保证一个事务的连接取自同一个,就无法保证事务的统一提交和回滚,
dataSourceUtils.getConnetion源码如下:
public static Connection doGetConnection(DataSource dataSource) 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("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(dataSource.getConnection()); } return conHolder.getConnection(); } // Else we either got no holder or an empty thread-bound holder here. logger.debug("Fetching JDBC Connection from DataSource"); Connection con = dataSource.getConnection(); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for JDBC Connection"); // Use same Connection for further JDBC actions within the transaction. // Thread-bound object will get removed by synchronization at transaction completion. ConnectionHolder holderToUse = conHolder; if (holderToUse == null) { holderToUse = new ConnectionHolder(con); } else { holderToUse.setConnection(con); } holderToUse.requested(); TransactionSynchronizationManager.registerSynchronization( new ConnectionSynchronization(holderToUse, dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != conHolder) { TransactionSynchronizationManager.bindResource(dataSource, holderToUse); } } return con; }
他是借助于TransactionSynchronizationManager取得连接,如果连接不存在,则从dataSource中获取连接,并且将新连接传递给TransactionSynchronizationManager,TransactionSynchronizationManager又做了什么呢?
ThreadLocal<Map<Object, Object>> resources public static void bindResource(Object key, 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 if none found if (map == null) { map = new HashMap<Object, Object>(); resources.set(map); } if (map.put(actualKey, value) != null) { throw new IllegalStateException("Already value [" + map.get(actualKey) + "] for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]"); } if (logger.isTraceEnabled()) { logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" + Thread.currentThread().getName() + "]"); } }
Resources是一个线程变量,每个线程都有一个Map,这个Map是以每一个数据源做Key的,也就是每一个线程中值存在一个数据源的一个连接,保证了在此次请求线程中公用一个线程池的一个连接,其实现事务的原理跟我们在之前演示的道理是一样的;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置DAO --> <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userDao" parent="transactionBase" > <property name="target" ref="userDaoTarget" /> </bean> </beans>
我们要享受事务的自动代理功能,就必须使用transactionBase代理的对象,他主要是通过了代理了service中的方法,如moneyAtoB,他被代理后生成了一个代理类,这个代理类被TransactionInterceptor进行了拦截,而TransactionInterceptor主要是对这个Dao方法进行了事务处理;
发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)