0

Hibernate Session

asked 2006-03-15 19:04:44 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3631399

By: oberinspector

is there a best practice to handle hibernate sessions and connection pools in ZK?

For tapestry i have used a hivemind service, which provides (creates) a hibernate session the first time a Session is used, starts a transaction and commits changes and deletes the session after the request is sent to the browser... (to avoid the lazy loading problem...) so dealing with the database objects is very easy:
...
//create some data
Person thomas = new Person();
thomas.setEmail("[email protected]");
thomas.setNickname("thomas");

//retrieve hibernate session
Session hs = getSessionOwner().getSession(); //tell hibernate to store the data hs.persist(thomas); ...
//send response and let the SessionOwner care for db commit and closing of the session ...
I have seen something like a sessionscope in the doku, which might be used...
In ZK not only the HTTP request can cause a Hibernate session, but also a ZK request...



delete flag offensive retag edit

34 Replies

Sort by ยป oldest newest

answered 2006-03-16 05:50:19 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3632131

By: tomyeh

Hi Oberinspector,

ZK uses an independent thread to process events. In other words, all applications codes execute in a thread other than the thread serving the request.

To handle auto-login and transaction, ZK provides two interfaces, EventThreadInit and EventThreadCleanup (in com.potix.zk.ui.event). To work with the connection pools, one shall implement EventThreadCleanup to close connections in case that developer might forget to do it.

I am planning to write a cleanup for MySQL+Tomcat, but I am new to this combination.
You might contribute one :)

link publish delete flag offensive edit

answered 2006-03-16 06:22:36 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3632145

By: oberinspector

probably i can contribute some things for hibernate, which is almost independent from the used db... the hibernate SessionOwner and SessionCreator i used with tapestry, use a database connection from a tomcat connection pool... The SessionOwner provides or creates (with the SessionCreator) the hibernate session and cares for commit and cleanup after the request has finished... so you don't have to care for commit and cleanup anymore...

I can post the SessionOwner and SessionCreator and the tomcat configuration...
probably we can have a look how this concepts could be integrated in ZK... until now my zk experience is only 2 days... so first i have to learn some things...

ZK looks really nice! Very good job!

link publish delete flag offensive edit

answered 2006-03-16 19:57:57 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3633446

By: oberinspector

i have written down some stuff Howto handle hibernate sessions:

http://en.wikibooks.org/wiki/ZK/How-Tos/HowToHandleHibernateSessions

This is not yet adapted to ZK, but describes some basic concepts and solutions i used in tapestry... I think with your help this could be adapted to ZK...

//thomas

link publish delete flag offensive edit

answered 2006-03-17 01:18:20 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3634320

By: tomyeh

Thanks. I'll take a look.

link publish delete flag offensive edit

answered 2006-03-17 17:18:20 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3635357

By: oberinspector

in the developers guide on page 35 there is an interface mentioned, which i can not find in the API... com.potix.zk.ui.event.EventThreadCleanup...

I'm looking for a place to inject a hibernate Session factory/owner in the event thread... and would like to have a place to cleanup the session... to get something like that:

-----------------------
->event... start thread
...
void method1(){
//create hibernate session the first time session owner is invoked....
Session hs = getSessionOwner().getSession();
//do something with hs
...
}
...
void method2(){
//retrieve existing hibernate session
Session hs = getSessionOwner().getSession();
//do again something with hs
...
//send response
}

//discard SessionOwner when thread finished:
end thread -> gesSessionOwner().threadDidDiscardService()
----------------------------------------------

It would be nice when every class which is invoked by a thread could access to SessionOwner...

Thank you for your help!

link publish delete flag offensive edit

answered 2006-03-17 17:48:04 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3635416

By: tomyeh

Please try ZK Nightly for EventThreadCleanup

link publish delete flag offensive edit

answered 2006-03-19 11:09:31 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3637842

By: oberinspector

i'm looking for a place to create a application wide service (hibernate) SessionCreator - a hibernate session factory... i can probably stuff the SessionCreator in the applicationScope, but is there central configuration place for this? web.xml? how? ...

on the other hand i need something like getSessionOwner() available in all my pages... the first time this function is called, a SessionOwner is created and the above mentioned SessionCreator is injected in SessionOwner (how?)... this new created SessionOwner is stuffed in the HTTP-Session (a good place?) and is provided in further getSessionOwner() calls... can i extend classes like page, desktop, window...??? what would be the best place to do this?

the cleanup can be done after finishing the event thread in EventThreadCleanup.cleanup() with something like
comp.getSession().getAttribute("SessionOwner").threadDidDiscardService() ???
the SessionOwner remains in the HttpSession, but the transaction and the hibernate session is closed...

where to make the hibernate session cleanup after a normal HTTP response?

ok... again a lot of questions... but i think a good hibernate integration would be a valuable feature for zk...

thank you for your help!!!

//thomas

link publish delete flag offensive edit

answered 2006-03-19 11:45:51 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3637865

By: tomyeh

Hi Thomas,

Let us try to resolve these issue. I have to ask you some questions back, because I'm new to Hibernate (we used in-house ORM for database access).

1. What is the scope of SessionCreator? One per app, one per HTTP-session, or one per transaction?

If one per app, you could implement javax.servlet.ServletContextListener and then register it in web.xml. Take LabelLocatorHook in pxweb.jar as an example.
Also take a look at zkdemo's web.xml for how to declare a listner.

If one per HTTP session, you could implement javax.servlet.http.HttpSessionListener
and then register it in web.xml. Take HttpSessionListener in zk.jar as an example.

If one per transaction, you have to use EventThreadInit to create it. With ZK, we suggest each transaction shall be ended in the same even listener, as depicted in pp. 95 in Developer's Guide.

Finally, you have to specify EventThreadInit and EventThreadCleanup in web.xml, pp 103 in Developer's Guide.

link publish delete flag offensive edit

answered 2006-03-19 12:46:40 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3637918

By: oberinspector

SessionCreator is one per application, so i will have a look in ServeletContextListener... this is a factory for hibernate sessions...

SessionOwner is probably one per session... not really shure until now... ;) it's a hibernate session provider... to have only one hibernate session per http session and to cleanup the hibernate session and transaction each time the conrtrol is back to the client... (thats where EventThreadCleanup comes
in...?)

thanks for that information! i will have a look and try my best...

regards, thomas

link publish delete flag offensive edit

answered 2006-03-19 12:57:37 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3637934

By: tomyeh

Good luck:)

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2006-03-15 19:04:44 +0800

Seen: 1,498 times

Last updated: Mar 23 '06

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More