google-code-export / hibernate-audit

Automatically exported from code.google.com/p/hibernate-audit
2 stars 1 forks source link

"Could not register synchronization for container transaction" exception when running hibernate-audit with Jboss AS and Spring #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
-What steps will reproduce the problem?
The application does not start - it fails with exception attached 

-What is the expected output? What do you see instead?
The expected output is application to start normally, as with previous version 
of Hibernate Audit (1.0.40)
Stacktrace is attached as a separate file.

-What version of the product are you using? On what operating system?
Produc version: Hibernate Audit 2.0.2, 
Other libraries : Hibernate 4.2.8 Final, Spring Framework 3.2.5.RELEASE
Application server: Jboss 7.1.1 Final
OS: Windows 7

-Please provide any additional information below.
Short summary of the issue:
1. Custom SpringTransactionSynchronization was implemented as described in 
Hibernate-audit manual: it is registered in Spring 
TransactionSynchronizationManager and wraps original synchronization parameter, 
providing order of HIGHEST_PRECEDENCE
2. Explicit listener's were removed from hibernate mapping file, because 
Hibernate Audit 2.0 automatically activates listeners
3. The error occurs even before SpringTransactionSynchronization. 
AuditSessionFactoryObserver is notified that SessionFactory is created. So 
AuditSessionFactoryObserver tries to initializeAuditMetatdata, and for this 
purpose, it begins a transaction (AuditSessionFactoryObserver:91)- at this 
point the exception is raised
3. Hibernate uses CMTransaction, and when trying to register synchonization, it 
goes to Jboss-specific classes that represent container's transaction, which in 
turn return transaction Status. STATUS_NO_TRANSACTION = 6, so synchronization 
registering is skipped.

Original issue reported on code.google.com by sergei.g...@gmail.com on 5 Feb 2014 at 12:37

GoogleCodeExporter commented 9 years ago
15:07:47,072 INFO  
[com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver] (MSC 
service thread 1-11) Start building audit log metadata.
15:07:47,100 INFO  
[com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver] (MSC 
service thread 1-11) End building audit log metadata.
15:07:47,319 ERROR [org.springframework.web.context.ContextLoader] (MSC service 
thread 1-11) Context initialization failed: 
org.springframework.beans.factory.BeanCreationException: nested exception is 
org.hibernate.TransactionException: Could not register synchronization for 
container transaction
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_07]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_07]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_07]
Caused by: org.springframework.beans.factory.BeanCreationException: Invocation 
of init method failed; nested exception is org.hibernate.TransactionException: 
Could not register synchronization for container transaction
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_07]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_07]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_07]
    at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_07]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1638) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1579) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 20 more
Caused by: org.hibernate.TransactionException: Could not register 
synchronization for container transaction
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.afterTransactionBegin(CMTTransaction.java:72) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:164) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1351) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.initializeAuditMetatdata(AuditSessionFactoryObserver.java:91) [hibernate-audit-2.0.2-api-v1.jar:2.0.2-api-v1]
    at com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.sessionFactoryCreated(AuditSessionFactoryObserver.java:65) [hibernate-audit-2.0.2-api-v1.jar:2.0.2-api-v1]
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:52) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:574) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 81 more
Caused by: org.hibernate.TransactionException: Could not register 
synchronization for container transaction
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.afterTransactionBegin(CMTTransaction.java:72) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:164) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1351) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.initializeAuditMetatdata(AuditSessionFactoryObserver.java:91) [hibernate-audit-2.0.2-api-v1.jar:2.0.2-api-v1]
    at com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.sessionFactoryCreated(AuditSessionFactoryObserver.java:65) [hibernate-audit-2.0.2-api-v1.jar:2.0.2-api-v1]
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:52) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:574) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) [spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 81 more

Original comment by sergei.g...@gmail.com on 5 Feb 2014 at 12:39

GoogleCodeExporter commented 9 years ago
I was able to overcome this issue partially (I have commented transaction 
context in AuditSessionFactoryObserver while updating audit metadata) with some 
modifications around AuditSynchronizationManager and AuditSynchronization (see 
patch for changes). It seems that we should still use 
com.googlecode.hibernate.audit.extension.syncronization.TransactionSyncronizatio
n interface to allow register custom implementation for projects using 
automatic Hibernate session management (via Spring integration) and 
container-manager transaction strategy. 

Original comment by evgeni.g...@gmail.com on 11 Feb 2014 at 3:18

Attachments:

GoogleCodeExporter commented 9 years ago
To update metadata in transaction in CMT environment transactional context is 
introduced for AuditSessionFactoryObserver (see patch). Reference 
implementation for Spring managed transactions might look as follows:

public class CustomTransactionalContext implements TransactionalContext {
    @Override
    @Transactional(rollbackFor = { Exception.class })
    public void runInTx(Session session, TxCallback callback) {
        callback.execute();
    }
}

Original comment by evgeni.g...@gmail.com on 12 Feb 2014 at 2:56

Attachments:

GoogleCodeExporter commented 9 years ago
fixed in version 2.0.2

Original comment by kchobant...@gmail.com on 31 Mar 2014 at 2:57