This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Newbie Hibernate Thread-local Transactions

Nov 19th, 2004, 01:20 AM

Hello,

This may sound silly, but how do I declare the creation of thread-local Hibernate sessions with simple JDBC transactions? I'm writing a relatively simple web application, and I need thread-local sessions to take advantage of Hibernate's lazy-loading proxies in my presentation layer. I need some sort of intercepter that will create a session before the dispatcher servlet passes control to my controller and close the session after the presentation layer returns.

I've been looking through all the documentation, but I understand very little about JTA or database clusters -- and the Spring source code makes even less sense. Help!

Comment

I set up a thread-local transaction using the interceptor and a HibernateTransactionManager. I have no idea if the transaction manager is actually working, as I didn't wire it to anything, but ... anyway ...

Reading data from the database works just fine, but when I try to save new objects to the database in my DAO, nothing happens.

In this case, the ID field in newSchool remains null (which is messing up my view, because it uses the resulting ID in a hyperlink), and nothing is written to the database. Any suggestions? I'm using PostgreSQL 7.4.6.

Comment

The log is filled with all sorts of select statements, but no insert statements or Hibernate exceptions. Also, the PostgreSQL sequence I use for generating keys is not advancing with each insert attempt. My mapping follows:

The application behaves like the transaction is never committed. Everything works when I use the SessionFactory directly -- without HibernateTemplate -- and manually commit my transaction or flush my session in the DAO.

Thanks again!

Matthew

P.S. Happy Thanksgiving!

Comment

I've been re-reading the documentation and looking through more Spring source code, and the whole transaction framework is beginning to make a lot of sense. However, I still can't get my application to write changes to the database. It looks like the transaction proxy should be committing after my DAO returns, but obviously something is amiss. (Oh how I wish I could set breakpoints in web applications...)

I modified my OpenSessionInViewInterceptor configuration so it would flush after my controller returns ...

I turned on the debug-level output in the logs. I can see the interceptor flushing my session with "0 insertions, 0 updates, 0 deletions to 0 objects," but I see no mention of any Hibernate transactions as I would expect from, e.g., org/springframework/orm/hibernate/HibernateTransactionManager.java:371, which should output, "Opened new session [...] for Hibernate transaction."

Matthew

Comment

That's strange indeed. I added some log output to my own code, and discovered that the doSubmit method in my controller is never invoked! For some reason, it isn't overriding the method from SimpleFormController ... nevermind that I copied the signature directly from SimpleFormController.java ...