简体   繁体   English

@Transactional注释不起作用

[英]@Transactional annotation doesn't work

ok, enough. 好,够了 I couldn't make this work. 我无法完成这项工作。 I am a newbie to Spring transactions and using the @Transactional annotation in my service to manage transactions. 我是Spring交易的新手,并且在服务中使用@Transactional批注来管理交易。 Below is my spring bean configuration file. 下面是我的spring bean配置文件。

<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

  <tx:annotation-driven transaction-manager="txManager"/>

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

  <jee:jndi-lookup id="dataSource" jndi-name="jdbc/myapp"/> 

  <!-- other <bean/> definitions here -->

</beans>

and I annotate my service: 并且我注释了我的服务:

@Transactional
public class MyServiceImpl implements MyService {
...
}

I notice two things 我注意到两件事

  1. The connection that I get in my DAO [using DataSourceUtils.getConnection(dsName)] has the autocommit enabled [true]. 我在DAO中获得的连接[使用DataSourceUtils.getConnection(dsName)]已启用自动提交[true]。
  2. As far as I debugged there doesn't look to be any transaction that has begun during my service method invocation. 据我调试,在服务方法调用期间似乎没有任何事务开始。

Anyone had this problem? 有人遇到这个问题吗?

I use Hibernate with the HibernateTransactionManager myself, so I'm not too familiar with how the JDBC transaction manager works. 我自己将Hibernate与HibernateTransactionManager一起使用,因此我不太了解JDBC事务管理器的工作方式。 Have you inspected the callstack to see whether or not TransactionInterceptor is in there ? 您是否检查了调用栈以查看是否存在TransactionInterceptor? If it is, then the transactional annotation is working, and you may just be missing something else. 如果是这样,则事务批注正在运行,并且您可能只是缺少了其他内容。 If you haven't looked for it, what makes you think that it's not working ? 如果您没有寻找它,什么使您认为它不起作用? To eliminate the obvious, have you explicitly set a setting in your JDBC configuration to disable autocommit ? 为了消除这种情况,您是否已在JDBC配置中显式设置了禁用自动提交的设置?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM