Steeve, the is the execution trace of the code that fails. I have debug information about everything related to transaction management and to session management.
You can see that the second attempt at persisting the entity is made with a new instance of a Session obtained through the SessionFactory.
Explanation: There is a class called SessionPerThreadStrategy that synchronizes a Session with a TreadLocal and manages references obtained. When no more references are held, the session is closed and the reference is never used again ( a new session is created upon the next request).
Execution trace:
Code:
//This is when attempting to save the entity at first
//The service aquires a reference to the session in order to start a transaction
2005-05-05 11:55:16,204 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Processing with transaction
2005-05-05 11:55:16,214 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Creating Transaction
2005-05-05 11:55:16,214 DEBUG SessionPerThreadStrategy.getSession - Acquiring session for thread: main
2005-05-05 11:55:16,214 DEBUG SessionPerThreadStrategy.getSession - Session does not exist yet; will create one
2005-05-05 11:55:16,214 DEBUG SessionPerThreadStrategy.createSession - Created an instance of session. Hashcode: 5076660
2005-05-05 11:55:16,214 DEBUG SessionPerThreadStrategy.sessionAcquired - Session was acquired; increasing number of references to: 1
2005-05-05 11:55:16,214 DEBUG SessionPerThreadStrategy.getSession - Returning instance of session with Hashcode: 5076660
2005-05-05 11:55:16,634 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Transaction created
2005-05-05 11:55:16,634 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Executing command
2005-05-05 11:55:16,634 DEBUG EntitlementManagementServicesImpl.saveRole - Saving a SecurityRole. name -> null
//The DAO class gets a reference to the session
2005-05-05 11:55:16,644 DEBUG SessionPerThreadStrategy.getSession - Acquiring session for thread: main
2005-05-05 11:55:16,644 DEBUG SessionPerThreadStrategy.sessionAcquired - Session was acquired; increasing number of references to: 2
2005-05-05 11:55:16,644 DEBUG SessionPerThreadStrategy.getSession - Returning instance of session with Hashcode: 5076660
//The exception is thrown - the DAO releases its reference to the session
2005-05-05 11:55:21,001 DEBUG SessionPerThreadStrategy.releaseSession - Releasing session for thread: main
2005-05-05 11:55:21,001 DEBUG SessionPerThreadStrategy.sessionReleased - Session was released; decreasing number of references to: 1
2005-05-05 11:55:21,001 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - ServiceException Error saving role with name:null
//The service rolls back the transaction
2005-05-05 11:55:21,021 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - TRANSACTION IS BEING ROLLED BACK
//The service rolls releases its reference to the session
2005-05-05 11:55:21,461 DEBUG SessionPerThreadStrategy.releaseSession - Releasing session for thread: main
2005-05-05 11:55:21,461 DEBUG SessionPerThreadStrategy.sessionReleased - Session was released; decreasing number of references to: 0
//The session is closed and no reference to it are held
2005-05-05 11:55:21,461 DEBUG SessionPerThreadStrategy.releaseSession - Session no longer in use; will be closed now
2005-05-05 11:55:21,822 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - TRANSACTION ROLLED BACK
//The entity is fixed (null values are set)
//The service acquires a reference to the session in order to start a transaction
2005-05-05 11:55:21,832 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Processing with transaction
2005-05-05 11:55:21,832 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Creating Transaction
2005-05-05 11:55:21,832 DEBUG SessionPerThreadStrategy.getSession - Acquiring session for thread: main
//A New session is created
2005-05-05 11:55:21,842 DEBUG SessionPerThreadStrategy.getSession - Session does not exist yet; will create one
2005-05-05 11:55:21,842 DEBUG SessionPerThreadStrategy.createSession - Created an instance of session. Hashcode: 29071960
2005-05-05 11:55:21,842 DEBUG SessionPerThreadStrategy.sessionAcquired - Session was acquired; increasing number of references to: 1
2005-05-05 11:55:21,842 DEBUG SessionPerThreadStrategy.getSession - Returning instance of session with Hashcode: 29071960
2005-05-05 11:55:22,603 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Transaction created
2005-05-05 11:55:22,603 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - Executing command
2005-05-05 11:55:22,613 DEBUG EntitlementManagementServicesImpl.saveRole - Saving a SecurityRole. name -> FailureTestRole1115308521832
//The DAO class gets a reference to the session
2005-05-05 11:55:22,613 DEBUG SessionPerThreadStrategy.getSession - Acquiring session for thread: main
2005-05-05 11:55:22,613 DEBUG SessionPerThreadStrategy.sessionAcquired - Session was acquired; increasing number of references to: 2
2005-05-05 11:55:22,613 DEBUG SessionPerThreadStrategy.getSession - Returning instance of session with Hashcode: 29071960
Hibernate: update security_role set role_name=?, created=?, modified=?, domain_id=? where id=?
//An exception is throw. The DAO releases its reference (this exception should not occur)
2005-05-05 11:55:22,973 DEBUG SessionPerThreadStrategy.releaseSession - Releasing session for thread: main
2005-05-05 11:55:22,973 DEBUG SessionPerThreadStrategy.sessionReleased - Session was released; decreasing number of references to: 1
//The services catches the exception and rolls back the transaction
2005-05-05 11:55:22,973 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - ServiceException Error saving role with name:FailureTestRole1115308521832
2005-05-05 11:55:22,984 DEBUG StandaloneEntitlementManagementServiceImpl.processWithTransaction - TRANSACTION IS BEING ROLLED BACK
//The service releases its session
2005-05-05 11:55:23,334 DEBUG SessionPerThreadStrategy.releaseSession - Releasing session for thread: main
2005-05-05 11:55:23,344 DEBUG SessionPerThreadStrategy.sessionReleased - Session was released; decreasing number of references to: 0
2005-05-05 11:55:23,344 DEBUG SessionPerThreadStrategy.releaseSession - Session no longer in use; will be closed now
And then the exception is throw which wraps the following:
Code:
Caused by: com.db.eps.common.persistence.DataAccessException: org.hibernate.HibernateException: Unexpected row count: 0 expected: 1
at com.db.eps.common.persistence.hibernate.support.AbstractHibernateDAO.convertToDataAccessException(AbstractHibernateDAO.java:69)
at com.db.eps.common.persistence.hibernate.support.AbstractHibernateDAO.process(AbstractHibernateDAO.java:51)
at com.db.eps.common.persistence.hibernate.support.AbstractHibernateDAO.saveOrUpdate(AbstractHibernateDAO.java:109)
at com.db.eps.common.entitlement.service.persistence.DAO.impl.HibernateSecurityObjectDAO.save(HibernateSecurityObjectDAO.java:44)
at com.db.eps.common.entitlement.service.facade.pojo.EntitlementManagementServicesImpl.saveRole(EntitlementManagementServicesImpl.java:142)
Thanks in advance for your help.