【问题标题】:why insert two same records and the transaction doesn't rollback为什么插入两条相同的记录并且事务不回滚
【发布时间】:2018-12-29 11:00:44
【问题描述】:

我做了一个javaweb的基础项目,在上面加了spring事务配置,但是没有效果。在此之前,它运行良好。我在网上搜索了很多页面,这些配置和我的一样。

预期结果是什么都不会插入,但插入了一条记录。

请帮我找出问题所在,谢谢

我的serviceimpl路径是:com.lidaning.sys.user.service.UserInfoServiceImpl

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

 <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="insert*" propagation="REQUIRED"/>    
        <tx:method name="*"/>
    </tx:attributes>
 </tx:advice>

 <aop:config>
    <aop:pointcut id="txPointcut" expression="execution(* com.lidaning.sys.user.service..*.*(..))" />
    <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
 </aop:config>

@Override
public void insertUser(UserInfo u) {
    u.setId("1");
    u.setName("lidaning");
    u.setPassword("***");
    userInfoDao.insertUser(u);
    userInfoDao.insertUser(u);  //occur exception 
}

【问题讨论】:

    标签: java transactions


    【解决方案1】:

    我明白了。因为spring两次加载了serviceBean(spring,springMVC),导致service丢失了事务的特性。当我将控制器与 applicationcontext 分离并放入 springMVC 时,它可以工作。

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多