Java Spring Hibernate事务回滚不工作

Java Spring Hibernate事务回滚不工作,java,spring,hibernate,transactions,rollback,Java,Spring,Hibernate,Transactions,Rollback,我正在从服务层调用两种不同的dao方法。 第二个方法引发空指针异常(“地址”列不能为空),但从第一个方法完成的任务无法回滚 服务层 package com.yetistep.dboy.business.impl; @Service @Transactional public class TestServiceImpl implements TestService { @Autowired TestDaoService testDao; @Autowired Cu

我正在从服务层调用两种不同的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;
    }
}  
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;
    }
}  
//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;
    }
}  
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;
    }
}  
//控制器

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中的事务配置
//数据源和hiibernate配置在此

<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包的任何方法抛出错误,那么应该执行回滚
有什么问题请回答我的问题

它为什么要回滚。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点

您的DAO在多个方面都是错误的

  • 决不要将会话/事务存储在实例变量中
  • 使用Spring管理事务时,切勿使用
    openSession
  • 如果需要容器管理的事务,请不要管理自己的事务
  • 总之,修复你的DAO

    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;
        }
    }  
    
    这就是您所需要的(当然,这适用于所有DAO)


    此外,您的配置也是错误的,您只需要
    您不需要

    以及为什么要回滚。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点

    您的DAO在多个方面都是错误的

  • 决不要将会话/事务存储在实例变量中
  • 使用Spring管理事务时,切勿使用
    openSession
  • 如果需要容器管理的事务,请不要管理自己的事务
  • 总之,修复你的DAO

    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;
        }
    }  
    
    这就是您所需要的(当然,这适用于所有DAO)


    此外,您的配置也是错误的,您只需要
    您不需要

    以及为什么要回滚。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点

    您的DAO在多个方面都是错误的

  • 决不要将会话/事务存储在实例变量中
  • 使用Spring管理事务时,切勿使用
    openSession
  • 如果需要容器管理的事务,请不要管理自己的事务
  • 总之,修复你的DAO

    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;
        }
    }  
    
    这就是您所需要的(当然,这适用于所有DAO)


    此外,您的配置也是错误的,您只需要
    您不需要

    以及为什么要回滚。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点

    您的DAO在多个方面都是错误的

  • 决不要将会话/事务存储在实例变量中
  • 使用Spring管理事务时,切勿使用
    openSession
  • 如果需要容器管理的事务,请不要管理自己的事务
  • 总之,修复你的DAO

    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;
        }
    }  
    
    这就是您所需要的(当然,这适用于所有DAO)


    此外,您的配置也是错误的,您只需要
    您不需要

    并且为什么要回滚。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点?为什么它会倒退。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点?为什么它会倒退。。。您将自己打开新会话,并自己管理事务。春天怎么会知道这一点?为什么它会倒退。。。您将自己打开新会话,并自己管理事务。Spring怎么会知道这一点呢?+1多亏了清晰的概念……还有一个问题是我想控制……impl包中的任何方法都从get或find开始,只执行select操作……这样就添加了and……控制的更好选项是什么?您想控制的是什么?您只需将
    readonly=true
    添加到这些方法的注释中(或者将write方法的默认值设置为readonly和override).+1感谢您提供了清晰的概念…还有一个问题是我想控制…impl包中的任何方法都从get或find开始执行仅选择操作…因此添加了and…控制的更好选项是什么?您想控制什么?您只需将
    readonly=true
    添加到这些方法的注释中(或者将write方法的默认值设置为readonly和override).+1感谢您提供了清晰的概念…还有一个问题是我想控制…impl包中的任何方法都从get或find开始执行仅选择操作…因此添加了and…控制的更好选项是什么?您想控制什么?您只需将
    readonly=true
    添加到这些方法的注释中(或者将write方法的默认值设置为readonly和override).+1感谢您提供了清晰的概念…还有一个问题是我想控制…impl包中的任何方法都从get或find开始执行仅选择操作…因此添加了and…控制的更好选项是什么?您想控制什么?您只需将
    readonly=true
    添加到这些方法的注释中(或者将write方法的默认值设置为readonly和override)。