【问题标题】:Spring @Transactional configuring xmlSpring @Transactional 配置xml
【发布时间】:2016-04-28 14:35:22
【问题描述】:

我的事务中的回滚不起作用(Spring 3.1)。我试图编辑我的配置 xml 文件,例如 here,但没有结果。 这是我的xml文件:

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:aop= "http://www.springframework.org/schema/aop"
        xmlns:tx= "http://www.springframework.org/schema/tx"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc.xsd 
                http://www.springframework.org/schema/jee   
                http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                ">
    
        <jee:jndi-lookup id="dataSourceUsrAppe1" jndi-name="jdbc/ZhabDS"/>
        
        <bean id="utentiDAO" class="it.dao.UtentiDAO">
            <property name="dataSourceUsrAppe1">
                <ref bean="dataSourceUsrAppe1"/>
            </property>
        </bean>
<!--    doesn't work with this:
        <tx:annotation-driven transaction-manager="txManager"/>
            <property name="dataSourceUsrAppe1">
                <ref bean="dataSourceUsrAppe1"/>
            </property>
        </bean>  
    </beans>
-->
</beans>

我应该在这里添加一个事务管理器吗?

这是我的服务:

@Service
public class UtentiService {
    @Autowired
    private UtentiDAO utentiDAO;

    @Transactional(rollbackFor={Exception.class}, propagation=Propagation.REQUIRED)
    public boolean createUser(Zhabuten user)  throws Exception
    {
            long idPrincipale;
            idPrincipale = utentiDAO.insert(user, utentiDAO.query1);
            idPrincipale = utentiDAO.insert(user, utentiDAO.query2);

            if (idPrincipale!=0) throw new java.lang.Exception();

            idPrincipale = utentiDAO.insert(user, utentiDAO.query3);
        return false;
    }
}

异常被正确抛出,它是从控制器中捕获的,并且数据库没有回滚。 我是否缺少 xml 中的任何配置?

【问题讨论】:

  • 你的事务管理器从何而来?
  • 你检查过日志吗?事务是否开始并提交?
  • 我不明白如何配置它。我应该删除这个吗?
  • 我删除了它,不能在服务器上运行,它给了我错误。

标签: java xml spring spring-mvc transactions


【解决方案1】:

使用以下 xml 配置。

<!-- Enable Annotation based Declarative Transaction Management -->
<tx:annotation-driven proxy-target-class="true"
    transaction-manager="transactionManager" />

<!-- Creating TransactionManager Bean, since JDBC we are creating of type 
    DataSourceTransactionManager -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceUsrAppe1" />
</bean>

【讨论】:

  • 谢谢,这行得通!奇怪的是,在此之后,我在调试模式下从 eclipse 中得到了一个新的奇怪弹出错误(之后我清理了所有旧断点): Unable to install breakpoint it.service.UtentiService$$...CGLIB...$$someHEXnumber 由于缺少行编译器属性。修改编译器选项以生成行号属性。原因:缺少行号信息。
猜你喜欢
  • 1970-01-01
  • 2019-11-01
  • 2011-05-10
  • 2019-10-08
  • 2023-03-21
  • 2015-02-02
  • 2018-08-02
  • 2013-10-24
  • 2011-11-26
相关资源
最近更新 更多