Hibernate version:
2.1.6
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="de.darklajid.cpos.model.UserSession" table="UserSession">
<id name="ID" type="long" unsaved-value="0">
<generator class="net.sf.hibernate.id.IdentityGenerator" />
</id>
<property
name="IP"
type="string"
/>
<property
name="LastAccess"
type="timestamp"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
UserSession _session = getSession(pRequest);
_session.setLastAccess(new Date());
_session.setIP(pRequest.getRemoteAddr());
try
{
mySession.saveOrUpdate(_session);
}
catch (HibernateException e)
{
e.printStackTrace();
}
Full stack trace of any exception that occurs:
na
Name and version of the database you are using:
MySQL 4.0.20
Debug level Hibernate log excerpt:
22:27:36,398 DEBUG SessionImpl:555 - opened session
22:27:36,412 DEBUG SessionImpl:1986 - loading [de.darklajid.cpos.model.UserSession#1]
22:27:36,413 DEBUG SessionImpl:2083 - attempting to resolve [de.darklajid.cpos.model.UserSession#1]
22:27:36,414 DEBUG SessionImpl:2116 - object not resolved in any cache [de.darklajid.cpos.model.UserSession#1]
22:27:36,416 DEBUG EntityPersister:416 - Materializing entity: [de.darklajid.cpos.model.UserSession#1]
22:27:36,417 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
22:27:36,421 DEBUG SQL:226 - select usersessio0_.ID as ID1_, usersessio0_.IP as IP1_, usersessio0_.LastAccess as LastAccess1_, user1_.ID as ID0_, user1_.Name as Name0_, user1_.Password as Password0_ from UserSession usersessio0_ left outer join User user1_ on usersessio0_.ID=user1_.ID where usersessio0_.ID=?
22:27:36,422 DEBUG BatcherImpl:249 - preparing statement
22:27:36,545 DEBUG Loader:277 - processing result set
22:27:36,547 DEBUG Loader:480 - result row: null, 1
22:27:36,548 DEBUG Loader:611 - Initializing object from ResultSet: 1
22:27:36,549 DEBUG Loader:680 - Hydrating entity: de.darklajid.cpos.model.UserSession#1
22:27:36,585 DEBUG Loader:294 - done processing result set (1 rows)
22:27:36,586 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
22:27:36,587 DEBUG BatcherImpl:269 - closing statement
22:27:36,590 DEBUG Loader:314 - total objects hydrated: 1
22:27:36,596 DEBUG SessionImpl:2202 - resolving associations for [de.darklajid.cpos.model.UserSession#1]
22:27:36,598 DEBUG SessionImpl:1986 - loading [de.darklajid.cpos.model.User#1]
22:27:36,599 DEBUG SessionImpl:2083 - attempting to resolve [de.darklajid.cpos.model.User#1]
22:27:36,600 DEBUG SessionImpl:2226 - done materializing entity [de.darklajid.cpos.model.UserSession#1]
22:27:36,601 DEBUG SessionImpl:3116 - initializing non-lazy collections
22:27:36,609 DEBUG SessionImpl:1372 - saveOrUpdate() persistent instance
22:27:36,615 DEBUG SessionImpl:2246 - flushing session
22:27:36,626 DEBUG SessionImpl:2439 - Flushing entities and processing referenced collections
22:27:36,629 DEBUG AbstractEntityPersister:275 - de.darklajid.cpos.model.UserSession.LastAccess is dirty
22:27:36,630 DEBUG SessionImpl:2533 - Updating entity: [de.darklajid.cpos.model.UserSession#1]
22:27:36,638 DEBUG SessionImpl:2780 - Processing unreferenced collections
22:27:36,639 DEBUG SessionImpl:2794 - Scheduling collection removes/(re)creates/updates
22:27:36,640 DEBUG SessionImpl:2270 - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
22:27:36,641 DEBUG SessionImpl:2275 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
22:27:36,642 DEBUG Printer:75 - listing entities:
22:27:36,653 DEBUG Printer:82 - de.darklajid.cpos.model.UserSession{User=null, IP=127.0.0.1, LastAccess=2004-08-11 22:27:36, ID=1}
22:27:36,655 DEBUG SessionImpl:1818 - Dont need to execute flush
Problem Report:
I'm using the code fragment shown above in a web application to store my own Implementation of a HttpSession. As you can see it's still quite dumb/simple. The function getSession(HttpServletRequest) returns a transient or persistent UserSession, depending on the Cookies I received. I made sure that loading my Sessions works, so does the creation of new UserSession-objects (transient->persistent). Looking at the code-fragment above I'm absolutely clueless why new Sessions are generated "the right way", old sessions are loaded/retrieved from the database but won't get updated in the DB. _session.setLastAccess(new Date()) should always make sure that the current time gets written to the database.
I'm using the ThreadLocal-Pattern from
www.hibernate.org, using the static method HibernateSession.currentSession() to get a threadlocal session and _session.flush; Hibernate.closeSession(); in a servlet-filter after all the work is done.
MySQL shows this:
mysql> select * from UserSession;
+----+-----------+---------------------+
| ID | IP | LastAccess |
+----+-----------+---------------------+
| 1 | 127.0.0.1| 2004-08-11 22:24:07 |
+----+-----------+---------------------+
1 row in set (0.00 sec)
Hibernate said:
LastAccess=2004-08-11 22:27:36
I'm completely lost. Thanks for any hint. I did read the FAQs and most parts of the hibernate reference - but I've still no clue.
Ben