Feb 13th, 2007, 09:55 AM
Alternative to OpenSessionInViewFilter for non-webapps
I was wondering if there is an alternative for the OpenSessionInViewFilter for non-webapps? I am using Hibernate and all of my DAO's extend HibernateDAO support. I need to have lazy loading capabilities, and I was wondering if there is anything that I can use to start a hibernate session at the beginning of a particular method (at the service layer) and close it at the end of the method. I want to make sure that whatever I use will be transparent to my DAO's.
I know that I can use the following code that I use in my Junit test cases, but I would prefer to leverage existing soutions if possible.
Session session = getSessionFactory().openSession();
TransactionSynchronizationManager.bindResource(get SessionFactory(), new SessionHolder(session));
Feb 13th, 2007, 10:44 AM
If you just need this around the service layer methods, why not wrap these methods with transactions, which will automatically take care of the session for you?
Feb 13th, 2007, 11:51 AM
I don't know why I didn't think of that. I use TransactionProxyFactoryBean on all of my DAO's, so all I have to do is configure the service the same exact way.
Thanks, I'll give it a try.
Feb 13th, 2007, 12:06 PM
If you are going to make the services transactional, you don't have to repeat it on the dao. Obviously unless you really need it.
Feb 13th, 2007, 12:15 PM
I have everything setup to join an existing transaction. At this point, I don't know if the DAO's will be used outside of the service.
Feb 20th, 2007, 02:28 AM