首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring @Transactional配置xml

Spring @Transactional配置xml
EN

Stack Overflow用户
提问于 2016-04-28 22:35:23
回答 1查看 9.3K关注 0票数 1

事务中的回滚不起作用(Spring 3.1)。我尝试编辑我的配置xml文件,就像here一样,但是没有结果。下面是我的xml文件:

代码语言:javascript
复制
    <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>

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

这是我的服务:

代码语言:javascript
复制
@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中的配置?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-29 17:26:03

使用以下xml配置。

代码语言:javascript
复制
<!-- 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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36917842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档