java - Spring Hibernate 事务回滚不起作用

标签 java spring hibernate transactions rollback

我正在从服务层调用2种不同的dao方法。 第二种方法抛出空指针异常(列“地址”不能为空),但第一种方法完成的任务无法回滚
服务层

package com.yetistep.dboy.business.impl;
@Service
@Transactional
public class TestServiceImpl implements TestService {
    @Autowired
    TestDaoService testDao;

    @Autowired
    CustomerDaoService customerService;

    @Override
    @Transactional //I supposed propagation control from aop config 
    public Boolean saveMulTransactions(User user, Customer customer) throws Exception {
        testDao.saveUser(user);

        customerService.saveCustomer(customer);

        return true;
    }
}  

//Dao层

public class TestDaoServiceImpl implements TestDaoService {
    @Autowired
    SessionFactory sessionFactory;
    Session session = null;
    Transaction tx = null;

    @Override
    public boolean saveUser(User user) throws Exception {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        session.save(user);
        tx.commit();
        session.close();
        return false;
    }
}
public class CustomerDaoServiceImpl implements CustomerDaoService{
    @Autowired
    SessionFactory sessionFactory;
    Session session = null;
    Transaction tx = null;
    @Override
    public boolean saveCustomer(Customer customer) throws Exception {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        session.save(customer);
        tx.commit();
        session.close();
        return false;
    }
}  

// Controller

public @ResponseBody
    ServiceResponse createAdmin(@RequestBody User user) throws Exception{
        log.debug("Saving User to Database");

        ServiceResponse serviceResponse = null;
        try {
            Customer customer = new Customer();
            customer.setName("Jeka");
            customer.setContact("87897898788978979");
            customer.setContact("Ktm");

            testService.saveMulTransactions(user, customer);
            serviceResponse = new ServiceResponse("User Added Successfully!!!");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return serviceResponse;
    }  

/xml 中的事务配置
//数据源和hibernate配置在这里......

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

    <!-- MUST have transaction manager, using aop and aspects  -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="userServicePointCut"
                      expression="execution(* com.yetistep.dboy.business.impl.*ServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut" />
    </aop:config>  

我认为,如果 impl 包的任何方法抛出错误,则应该执行回滚 有什么问题请通过回答建议我吗?

最佳答案

为什么它应该回滚...您自己打开新 session 并自己管理事务。 Spring 如何知道这一点。

你的 daos 在很多方面都是错误的。

  1. 切勿将 session /事务存储在实例变量中
  2. 切勿使用openSession使用 Spring 管理事务时
  3. 如果您希望由容器管理事务,则切勿管理自己的事务。

所以简而言之,修复你的 daos。

public class CustomerDaoServiceImpl implements CustomerDaoService{
    @Autowired
    SessionFactory sessionFactory;

    @Override
    public boolean saveCustomer(Customer customer) throws Exception {
        Session session = sessionFactory.getCurrentSession();
        session.save(customer);
        return false;
    }
}  

这就是您所需要的(当然这适用于您所有的 daos)。

另外你的配置也是错误的,你只需要 <tx:annotation-driven />你不需要<tx:advice /><aop:config />删除那些。

所有这些都在the reference guide中进行了解释。 .

关于java - Spring Hibernate 事务回滚不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26924569/

相关文章:

java - hibernate 主键的原语或包装器

hibernate - "Where query"在生产中创建错误的 SQL

java - 无法加载请求的类: org. hibernate.MySQL5InnoDBDialect

java - Session 类型中的 setConfig(Properties) 不适用于参数 (String, String)”

java - 使用 JPA 创建唯一并合并数据库中的现有行

java - 在提交之前验证属性/YAML 文件中的值

spring - 如何更新Spring Security Management Console?

java - GXT - 如何将网格过滤器参数添加到请求 URL(获取参数)?

mysql 两个FK在同一个表中

Java hibernate如何使用select...where id in()