Yeah. With GenericDAO you need to explicitly open a transaction before and close it after calling DAO methods. Generally transactions are bound to sessions. The DAO uses the current active session. (You can make as many DAO calls as you want within a transaction/session.) One standard practice is to have a service layer in your application that controls the transactions and calls the DAO methods.
GenericDAO makes the assumption that you will be handling transactions externally to the DAO. But there are a few customization points if this is not the behavior you desire. (1) You can override getSession() (2) you can override all the methods and add transaction handling code before and after calling through to GenericDAOImpl (3) you can use some kind of Aspect Oriented Programming library to automatically open and close transactions around all your DAO methods.
Now let me go back and give one very basic example of how you might handle transactions externally.
Session session = null;
Transaction tx = null;
try{
session = sessionFactory.openSession(); // This sessionFactory is the same you set on hamburgerDao.setSessionFactory()
tx = session.beginTransaction();
tx.setTimeout(5);
Hamburger hamburger = hamburgerDao.search(123);
hamburger.weight = .25;
tx.commit();
}catch(RuntimeException e){
try{
tx.rollback();
}catch(RuntimeException rbe){
log.error("Couldn’t roll back transaction", rbe);
}
throw e;
}finally{
if(session!=null){
session.close();
}
}
Hope this helps.
- David W.