Some clarification about Spring @Transactional annotation on a method

前端 未结 2 1099
抹茶落季
抹茶落季 2020-12-08 00:20

I am quite new in Spring world and I have developed a simple project that use Spring 3.2.1 and Hibernate 4.1.9 to implement a DAO. The project work correctly but I have some

2条回答
  •  盖世英雄少女心
    2020-12-08 00:58

    First of all, you shouldn't make DAO methods transactional, but service methods.

    Second, using Transactional is a way to let Spring start and commit/rollback transactions for you. So you shouldn't start and commit transactions yourself.

    Third: this will only work if you use a transaction manager that knows how to associate a Hibernate session with the transaction (typically, a HibernateTransactionManager). The session factory should also be handled by Spring, and injected by Spring in your DAOs. The code of the DAO should look like this:

    Fourth: you should not open a new session, but get the current one, associated to the current transaction by Spring.

    public class PersonDAOImpl implements PersonDAO {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public Integer addPerson(Person p) {
            Session session = sessionFactory.getCurrentSession();
            Integer personID = (Integer) session.save(p);
            return personID;
        }
    
        public Person getById(int id) {
            Session session = sessionFactory.getCurrentSession();
            Person retrievedPerson = (Person) session.get(Person.class, id);
            return retrievedPerson;
        }
    
        @SuppressWarnings("unchecked")
        public List getPersonsList() {
            Session session = sessionFactory.getCurrentSession();
            Criteria criteria = session.createCriteria(Person.class);
            return criteria.list();
        }
    
        public void delete(int id) {
            Session session = sessionFactory.getCurrentSession();
            Person personToDelete = getById(id);
            session.delete(personToDelete);
        }
    
        public void update(Person personToUpdate) {
            Session session = sessionFactory.getCurrentSession();
            session.update(personToUpdate);
        }
    }
    

    Read the documentation for more information.

提交回复
热议问题