Hello,
I am working on Java SE desktop application and use Hibernate for database access.
In my application i have list of objects (beans) and user is capable to add/remove/modify objects from list. List is displayed in one panel and add/modify dialog is opened when user add new object to the list. User can wait as long as he wants on add/modify dialog before commit changes. After new object is added i need to refresh list to include new object.
My idea is to open new session when display list at startup to load all data from database. session = SessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); Transaction tx = session.beginTransaction(); load(foo); tx.commit();
I suppose session is now released from JDBC connection and returned to pool?
Then when add/modify dialog is opened i forward same session to add/modify dialog. When user fill form and press submit button here is what i do: Transaction tx = session.beginTransaction(); // this is same session as previous fooDao.persist(newFoo); session.flush(); // This is last transaction in conversation tx.commit(); // Also return JDBC connection session.close();
After that i come back to list of objects again. If i close session as it done after persisting new object i cannot access anymore my objects on list because session is closed. But if i don't close session, when i am get back on list everything works just fine and new objects are added to list but i am not sure is session disconnected now and returned to pool.
What i want is to enable user to list all objects (some objects have other objects lazily loaded) and to add/modify objects but without keeping session connected to JDBC connection all time, but only when is needed.
Is there any idea how to accomplish this in hibernate?
|