十三、Spring中Xml声明事务
最后更新于:2022-04-01 11:38:33
补充知识点:
事务的传播特性:在当前的执行环境中。如果有多个方法嵌套互相调用的话,那么事务的特性必将从第一个方法传播到第二个、第三个。。。。
~~~
/*事务的传播性。如果当前执行环境中有事务,那么则会一直在
* 传播环境中传播下去。如果没有事务那么则会创建一个事务
* 默认就是required
* 而readOnly属性则是规定该事务只能是一些select操作。而insert,update等
* 动作是不能执行的。因为readonly的限制,而且拿到readonly=true的transation
* 的话它的执行效率是比前者高的
* 还有更多事务配置请在:
*http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-strategies
* 9.5.6.1. @Transactionalsettings 中查看
*/
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public void save(User u){
u.setName("zhanglong");
UserLog log = new UserLog();
log.setMsg("useradded");
userDaoImpl.save(u);
userLogDaoImpl.save(log);
}
~~~
下面看一个xml配置事务的案例:
1、 编写业务逻辑操作的简单方法:
~~~
@Component("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDaoImpl;
@Resource
private UserLogDao userLogDaoImpl;
public UserLogDao getUserLogDaoImpl() {
return userLogDaoImpl;
}
public void setUserLogDaoImpl(UserLogDao userLogDaoImpl) {
this.userLogDaoImpl = userLogDaoImpl;
}
public UserDao getUserDaoImpl() {
return userDaoImpl;
}
public void setUserDaoImpl(UserDao userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}
public void save(User u){
u.setName("zhanglong");
UserLog log = new UserLog();
log.setMsg("user added");
userDaoImpl.save(u);
userLogDaoImpl.save(log);
}
}
~~~
2、 编写XML配置文件,将事务加上去:
下面的文件主要包括配置数据库和指定事务前一段不用看。我们主要看事务在xml中的配置
1、 配置事务管理器transactionManager
2、 配置事务要管理的方法tx:advice
3、 配置AOP将逻辑事务织入到相应的方法上去
~~~
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<!-- 配置容器资源扫描的包 -->
<context:component-scan base-package="com.spring" />
<!-- 将前面类写入容器 -->
<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />
<!--
配置数据源 <bean id="myDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName"
value="com.mysql.jdbc.Driver"/> <property name="url"
value="jdbc:mysql://localhost:3306/sms"/> <property name="username"
value="root"/> <property name="password" value="root"/> </bean>
-->
<!-- placeholder 占位符 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 配置dataSource -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 将配置好的dataSource注入到SessionFactory中-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/spring/model/user.hbm.xml</value>
<value>com/spring/model/userlog.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<!-- ---------------------------以上是数据源和sessionFactory的配置 ----------------------------- -->
<!-- 声明式事务管理,事务需要数据源,从sessionFactory中拿到
这是一个AOP的应用 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务要管理的方法 -->
<tx:advice transaction-manager="transactionManager" id="txManager">
<tx:attributes>
<tx:method name="save" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop设置切面和织入点逻辑 -->
<aop:config>
<aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>
<aop:advisor
advice-ref="txManager"
pointcut-ref="entryPointMethod"
/>
</aop:config>
</beans>
~~~
### 文档参考Spring官方文档的:9.5.8. Advisingtransactional operations
[http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-declarative-applying-more-than-just-tx-advice](http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-declarative-applying-more-than-just-tx-advice)