When loading a domain object using hibernate and not modifying it, do I still need to define a spring transaction at the service layer?
I am currently learning Spring and Hibernate and have a question on transactions:
If I have an example DAO as follows:
@Override
public DatabaseObject getDatabaseObject(int id) {
Session session = sessionFactory.getCurrentSession();
return (DatabaseObject) session.createCriteria(DatabaseObject.class).add(Restrictions.eq("example", 3)).uniqueResult();
}
And in my service layer I have an example method
@Override
public int someBusinessLogic(int number) {
DatabaseObje开发者_运维技巧ct object = dao.getDatabaseObject(number);
//some business logic none of which modifies my databaseobject
}
Do I need to make someBusinessLogic transactional, and if so why?
You can get away without an explicit transaction but you will run into Exceptions if you have lazy relationships/collections from the DatabaseObject
and try to access them.
精彩评论