当要求多个事务要么同时完成,要么同时无法完成时,就涉及到了事务的处理.
Spring提供了声明式事务管理,其原理是通过AOP来完成的。
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
tx就是事务管理
并引入相应的命名空间。xml配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.dw"/>
<!-- <aop:aspectj-autoproxy/> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/dw/config/User.hbm.xml</value>
<value>com/dw/config/Log.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id = "userDaoImpl" class="com.dw.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id = "logDaoImpl" class = "com.dw.impl.LogDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id ="service" class = "com.dw.service.ServiceUser">
<property name="userDao" ref="userDaoImpl"/>
<property name="logDao" ref="logDaoImpl"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!--
<bean id = "iterception" class="com.dw.model.Iterception"/>
<aop:config>
<aop:pointcut expression="execution(* com.dw.car.*.*(..))" id="point"/>
<aop:aspect id="aspect" ref="iterception">
<aop:before method="before1" pointcut-ref="point"/>
<aop:after-returning method="after1" pointcut-ref="point"/>
<aop:around method="around1" pointcut="execution(* com.dw.car.*.*(..))"/>
</aop:aspect>
</aop:config> -->
</beans>
在相应的处理方法上使用注解@transactional
@Transactional
public void add(User user) {
userDao.save(user);
Log log = new Log();
log.setName("您插入了一条数据!");
logDao.save(log);
}
然后通过getCurrentSession()获取Session对象。