为了账号安全,请及时绑定邮箱和手机立即绑定

我配置spring事务为什么不起作用

/ 猿问

我配置spring事务为什么不起作用

45度呼吸 2019-03-03 04:00:34

application.xml 配置-------------------------------

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>

<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
service 配置------------------
<bean id="loginService" parent="txProxyTemplate"> 
<property name="target"> 
<bean 
class="com.play.login.service.impl.LoginServiceImpl"> 
<property name="loginDao" ref="loginDao" />
</bean> 
</property> 
</bean>

查看完整描述

2 回答

?
largeQ

对loginService声明事务时的方式有问题。按我下面的写法试试,应该没有问题的,我们项目就是这么写的。

<bean id="loginServiceTarget" class="yourLoginServiceClass" />
<bean id="loginService" parent="txProxyTemplate" >
<property name="target"><ref local="loginServiceTarget"/></property>
</bean>

查看完整回答
反对 回复 2019-03-10
?
达令说

事务直接配到DAO
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="baseTxProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id="studentDaoProxy" parent="baseTxProxy">
<property name="target">
<ref bean="studentDao" />
</property>
</bean>

<bean id="studentDao" class="com.dao.StudentDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>

StudentDao st = (StudentDao) context.getBean("studentDaoProxy");

两个bean也可以合并为
<bean id="studentDao" parent="baseTxProxy">
<property name="target">
<bean class="com.dao.StudentDaoImpl">
<property name="dataSource" ref="dataSource" />
<property name="kpiDao" ref="kpiDao" />
</bean>
</property>
</bean>
StudentDao st = (StudentDao) context.getBean("studentDao");
上述这种方式必须使用接口,为什么。

第二种同样必须用接口。配置起来比第一种麻烦
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="studentDaoProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="studentDao" />
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<bean id="studentDao" class="com.dao.StudentDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao st = (StudentDao) context.getBean("studentDaoProxy");

如果使用的都是接口,那么就不需要用cglib-nodep-2.1_3.jar
如果service调dao没有用到接口,那么必须用cglib-nodep-2.1_3.jar


查看完整回答
反对 回复 2019-03-10

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信