Hi!

Very much text and technical stuff, sorry for this.

The basic spring conversation scope is finished and I started to use it
in our project. Though, without persistence for now.

<do not flame>
For those of us, directly using hibernate objects in the view layer
</do not flame>
it would be great to synchronize a hibernate session with the lifetime
of a conversation (if its a conversation which requires persistence,
completely optional for sure)

I am somehow out of idea what could be the best idea to do design it. So
- help is welcome.


Ok, lets try to describe what I have in mind currently:

Each bean put into the spring conversation scope IS A conversation.
The bean lives there until the user ends the conversation or no one
accessed the bean within a given timeout.

Now, one is able (or should be) to load hibernate objects and put them
into the conversation bean.
Regardless if there is a new request or a pageflow, the conversation
bean is always the same and thus should use the same hibernate session.

This makes it clear, that the conversation bean should hold the
hibernate session too. This e.g. can be done by using spring injection, no?
Now, each DAO has to pick this session and issue hibernate calls.

Has someone an idea how the DAO can pick up such a session?

For now, I see two ways (spoken in scematic code):


In any case, every access has to be routed through the conversation bean:

1)
public class MyConversationBean
{
    HibernateSession session; [1]

    public List loadData()
    {
        return MyDataDAO.getInstance(session).loadData(); [2]
    }
}
[1] Spring injects the session.
[2] Conventional lookup of the DAO with pass in the session.

or

2)
public class MyConversationBean
{
    HibernateSession session; [1]
    MyDataDAO myDataDao; [2]

    public List loadData()
    {
        return myDataDao.loadData();
    }
}

[1] Spring injects the session.
[2] Spring injects the DAO too. Now it would be nice if spring could be
able to inform the MyConversationBean about the injected DAO so that the
conversation bean itself could inject the current session into the DAO.
Is Spring able to do it?
The HibernateSession could be hidden behind an base class, e.g. called
PersistentConversation

public class MyConversationBean extends PersistentConversation
{
    MyDataDAO myDataDao;

    public List loadData()
    {
        return myDataDao.loadData();
    }
}

public class PersistentConversation
{
    HibernateSession session;

    public void injectedBean(Object instance)
    {
         ((DAOBase) instance).setSession(session);
    }

    further commit/rollback handling
}

For sure, in the end I wont use HibernateSession, but an abstract class
where you can pack in a transaction, session or whatever access class
you want to. Its just to get the idea.

Now, what do you think? Any better way to do it?


Ciao,
Mario

Reply via email to