wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Of Hibernate sessions and transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Of Hibernate sessions and transactions" Watch "Of Hibernate sessions and transactions" New topic
Author

Of Hibernate sessions and transactions

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

I'm having a bit of a problem implementing an OSIV setup and I've traced it down to some heartburn I'm having with Hibernate sessions and transactions.

Given the following sample code:

I'm rather surprised to find that the log statement of line [4] emits false. I have the following in my Hibernate config file:

so shouldn't the session be maintained in thread-local storage?

Lines [5] and [6] show that tx and sf.getCurrentSession().getTransaction() reflect different Transaction instances, one active one not -- which is not surprising if there are two different Session instances.

What vital concept am I missing? I had assumed that once a session and transaction were established as in lines 1 through 3, that I'd be able to fetch the session and transaction later on down the road to use for queries and the like.

Obviously, without getting this working, an OSIV setup is not possible.

P.S. I'm not using EJBs or a JEE container, so no JTA.
[ May 01, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Does it influence your thinking to know that line 4 in the following code returns true? A consistent use of getCurrenctSession() seems to generate two logically equal instances, whereas using openSession() does not.






Not sure if this answers any questions you have, or if it just contributes to the talking point?

getCurrentSession
Session getCurrentSession() throws HibernateException

Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.

Note that for backwards compatibility, if a CurrentSessionContext is not configured but a JTA TransactionManagerLookup is configured this will default to the JTASessionContext impl.



Session openSession(Connection connection)

Open a Session on the given connection.

Note that the second-level cache will be disabled if you supply a JDBC connection. Hibernate will not be able to track any statements you might have executed in the same transaction. Consider implementing your own ConnectionProvider.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

I guess I'm still confused as to what's happening in my code snippet. If a session is opened with openSession() I'd assume that a new session is created. I'd then expect a call to getCurrentSession() to return that session rather than create a new one. If a new one is created by getCurrentSession() but subsequent calls to getCurrentSession() return that same session, under what conditions does getCurrentSession() create a new session and when does it return the existing session?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Bear Bibeault:
I guess I'm still confused as to what's happening in my code snippet. If a session is opened with openSession() I'd assume that a new session is created. I'd then expect a call to getCurrentSession() to return that session rather than create a new one. If a new one is created by getCurrentSession() but subsequent calls to getCurrentSession() return that same session, under what conditions does getCurrentSession() create a new session and when does it return the existing session?


What I think is being said is that openSession() and getCurrentSession() don't talk to each other, or should I say, openSession() just returns you a Session and doesn't do anything internally to store it in some variable, so that it can know to either create a new Session or return the one already created.

Also meaning that is you are going to use getCurrentSession() Hibernate expects that that is all you are going to be calling, that you would never be calling openSession() at all, even at first.

Or easier put, lets look at the Hibernate code for openSession() at least the version of the method that all the other versions call.



It basically creates new and returns it on the same line.

Now if we look at the getCurrentSession() method it is actually looking to use a currentSessionContext, which stores the Session, or creates new, if the context hadn't created a Session yet.



This is definitely just a design decision, that does a great job of seperation of concerns here. Basically saying why add the whole extra context in openSession() if it isn't necessary, since most people will be using openSession in their context of not using getCurrentSession. Also to keep openSession() backwards compatible with older code written to older Hibernate code.

And I am also just conjecturing here, not having been in the room when the Hibernate guys made their design decisions.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Thanks guys! That really helps.

So we use either current or open, but not both. That wasn't made clear in the API or in the Bauer book. (Or if it was, I missed it).
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Originally posted by Bear Bibeault:

So we use either current or open, but not both. That wasn't made clear in the API or in the Bauer book. (Or if it was, I missed it).


I found this useful when i was trying to understand about transactions and sessions in Hibernate. That has a section "Transaction demarcation with plain JDBC" (when JTA is not used) which explains the difference of using openSession() and getCurrentSession().


[My Blog] [JavaRanch Journal]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Of Hibernate sessions and transactions
 
Similar Threads
Connection pool issue
Container managed transaction (CMT) doesn't commit transactions
Question regarding servlet filter for implementing the OSIV pattern...
Question about currentSession and Session-per-request in a Filter
JBoss 4.0.2 and Hibernate