Announcement Announcement Module
No announcement yet.
Hibernate Transactions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Transactions


    I've been investigating using spring's transaction annotations with hibernate. Right now we're using the hibernate threadlocal design pattern with an in house filter that handles closing hibernate sessions without using spring's transactionManager.
    In some places in our code we explicitly commit to generate a primary key because subsequent processing depends on it.

    So my question is if I implement OpenSessionInViewFilter, or the equivalent interceptor, and get my hibernate session via the SssionFactoryUtils can I still call session.beginTransaction.commit() in my dao's without any adverse affects? Will that call commit on spring's transactionManager or will that be a different transaction?


  • #2

    The OSIV filters/interceptors are completely separate from the transaction management interceptors. Our OSIV code simply starts and closes sessions. the TX interceptors obviously take care of managing the transactions.

    So, you can still use OSIV and begin/end transactions yourself, but you probably don't want to do this when using Spring TX interceptors.

    However, for your particular use case you should consider using flush() rather than commiting the transaction just to get the ID. If you were to use HibernateTemplate you can call HibernateTemplate.flush() to send all outstanding changes to the DB and update any IDs *without* having to commit the transaction.