Spring的事务,经典配置(转载)
Spring的事务
今天对 spring 的 AOP 事务有了一个新的认识,所以赶紧把今天的学习记下来,希望在今后的学习中能够起到一些作用,也能对今天的认识做一次总结。
1.spring 分享
先看一段代码:
Connection conn = Conn.getConnection(); conn.setAutoCommit(false); …….. ……... conn.rollback(); conn.commit();
public Long addLineItem(Long orderId, LineItem lineItem){ log("OrderListDAOHibernate.addLineItem : Start..."); OrderList orderList = (OrderList) getHibernateTemplate().load(OrderList.class, orderId); lineItem.setOrderList(orderList); getHibernateTemplate().saveOrUpdate(lineItem); getHibernateTemplate().saveOrUpdate(orderList); log("OrderListDAOHibernate.addLineItem : Ending..."); return lineItem.getId(); }protected Session getSession() { if (isAlwaysUseNewSession()) {return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()); } else if (!isAllowCreate()) {return SessionFactoryUtils.getSession(getSessionFactory(), false); } else {return SessionFactoryUtils.getSession( getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator()); } }SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Connection con = DataSourceUtils.getConnection(getDataSource());
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
<bean id="sessionFactory" /> </property> <property name="mappingResources"> <list> <value>mf/org/user/User.hbm.xml</value> </list> </property></bean><bean id="transactionManager" /> </property> </bean><bean id="txProxyTemplate" abstract="true" /> </property> <property name="transactionAttributes"> <props><prop key="save*">PROPAGATION_REQUIRED,-Exception</prop><prop key="remove*">PROPAGATION_REQUIRED,-Exception </prop><prop key="update*">PROPAGATION_REQUIRED,-Exception </prop><prop key="incress*">PROPAGATION_REQUIRED,-Exception </prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean><bean id="userManager" parent="txProxyTemplate"> <property name="target" ref="userManagerTarget" /></bean><bean id="userManagerTarget"ref="userDAO" /></bean><bean id="userDAO" ref="sessionFactory" /></bean>
<bean id="transactionManager" /> </property></bean>
<bean id="txProxyTemplate" abstract="true" /> </property> <property name="transactionAttributes"> <props><prop key="save*">PROPAGATION_REQUIRED,-Exception</prop><prop key="remove*">PROPAGATION_REQUIRED,-Exception </prop><prop key="update*">PROPAGATION_REQUIRED,-Exception </prop><prop key="incress*">PROPAGATION_REQUIRED,-Exception </prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property></bean>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop><prop key="remove*">PROPAGATION_REQUIRED,-Exception </prop><prop key="update*">PROPAGATION_REQUIRED,-Exception </prop><prop key="incress*">PROPAGATION_REQUIRED,-Exception </prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
<bean id="userManager" parent="txProxyTemplate"> <property name="target" ref="userManagerTarget" /></bean><bean id="userManagerTarget"ref="userDAO" /></bean><bean id="userDAO" ref="sessionFactory" /></bean> 当然我们也可以写成:<bean id="userManager" parent="txProxyTemplate"> <property name="target"> <bean ref="sessionFactory" /></bean>