Lazy Loading Objects
I am writing an application that will be using Spring-RCP for the user front-end, but not for the cron'd processes at night on the server. I have read many articles on Spring, DAOs, and lazy-loading, but the true answer seems to evade me.
I have my setup wired with a data source to HSQLDB (moving to Oracle once more of the application has been developed), LocalSessionFactoryBean to Hibernate, HibernateInterceptor, HibernateTransactionManager, TransactionInterceptor, and a ProxyFactoryBean.
In the DAO, I'm obtaining my own Session object from the SessionFactoryUtils class for each method which needs to read/write to the session.
In my Junit tests, I have a setUp and tearDown which opens and closes the Hibernate session as per an article I read on Karl Baum's blog. The tests work great with the lazy loading.
If this were a web application, I could just use the OpenSessionInView(Interceptor/Filter), but that still doesn't help with my server processes which wouldn't run in the servlet context nor through a SimpleUrlHandlerMapper.
So with my two requirements, Spring-RCP and cron'd server processes, what is the best way to wrap a session around my processes? I have looked at AOP, but I have no idea how that is wrapped around a method call.
Any tips or direction would be greatly appreciated.
I have the same doubt than you.
I don't know how to mantain the same session during a unit of work. It has to get the session on a service method (where start the transaction) and use it during the execution of this method. Daos and other services called by this has to use the same Session.
Let's clarify your architecture a little. You say, "conr'd processes at night on the server". This seems to indicate that you have some server process that runs at night - and it looks like this server process is written in Java + Spring? On the client side you have a Spring rich client based UI. So, what does this UI use to access data? Does it communicate with a middle tier via RPC or web services? Or does it directly connect to the DB? In other words, where are the DAOs located, in the client process or in some server process? I'm guessing that any "server process" wouldn't be the same thing as the "cron'd processes" since cron'd processes typically aren't running 24/7, making them unavailable as service providers to a rich client.
But I can at least clarify a little how Spring interacts with Hibernate. Basically, Spring associates Hibernate Sessions to transactions on a per-thread basis. In other words, if you want to retrieve the same Hibernate Session from SessionFactoryUtils across several method calls you have to do two things: 1) ensure the calls happen in the same thread, and 2) perform them in the same transaction. So, probably whats missing from your setup is a proxy that surrounds your "unit" with a Spring managed transaction. Once your architecture is clarified we can figure out exactly what "unit" means. I should note that if "unit" = a long lasting user interaction session then you probably won't be able to surround it with a transaction and will have to approach the problem from a different angle.