Spring 4 + JPA (Hibernate 4) + JTA transaction manager doesn't flush automatically - java

I am migrating an application from
Spring 3.0.5 + JPA 2.0
to
Spring 4 + JPA (Hibernate 4)
I have followed the migration guide : https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework.
The application is using a JTA transaction manager : a Jencks / GeronimoPlatformTransactionManager (because of transactions distributed on datasources and ESB).
The Spring / JPA configuration is :
<bean id="rduEntityManagerFactory" class="ch.vd.dsas.rdu.repository.crud.service.ExtendedLocalContainerEntityManagerFactoryBean"
depends-on="rduTransactionManagerLocator,jGroupsCacheManagerPeerProviderFactoryLocator">
<property name="persistenceUnitName" value="rduPersistenceUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${rdu.jpa.database}" />
</bean>
</property>
<property name="persistenceUnitPostProcessors">
<bean class="ch.vd.dsas.rdu.commons.tx.spring.JtaPersistenceUnitPostProcessor">
<property name="jtaDataSource" ref="rduDataSource" />
</bean>
</property>
<property name="jpaProperties" ref="jpaProperties"/>
</bean>
<util:properties id="jpaProperties">
<prop key="javax.persistence.transactionType">JTA</prop>
<prop key="javax.persistence.validation.mode">CALLBACK</prop>
<prop key="hibernate.hbm2ddl.auto">${rdu.jpa.hbm2ddl.auto}</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<!-- Transaction properties -->
<prop key="hibernate.transaction.jta.platform">ch.vd.dsas.rdu.ref.transaction.jencks.JencksTransactionManagerLookup</prop>
<prop key="hibernate.transaction.manager_lookup_class">ch.vd.dsas.rdu.transaction.jencks.JencksTransactionManagerLookup</prop>
<prop key="hibernate.default_schema">${rdu.datasource.schemaMetier}</prop>
<!-- Debug properties -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- Cache properties -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.ReplicatedSingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.cluster_name">${rdu.hibernate.cache.jgroups.cluster.name}</prop>
<prop key="net.sf.ehcache.configurationResourceName">/hibernate-ehcache.xml</prop>
</util:properties>
Transactions are annotation driven :
<tx:annotation-driven transaction-manager="rduJtaTransactionManager" />
The transaction manager is declared like that :
<!-- From Jencks org.jencks:jencks:2.2 -->
<bean id="rduJencksTransactionManager" class="org.jencks.factory.TransactionManagerFactoryBean" />
<bean id="rduJtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<qualifier value="rdu" />
<property name="transactionManager" ref="rduJencksTransactionManager" />
<property name="userTransaction" ref="rduJencksTransactionManager" />
</bean>
<bean id="rduTransactionManagerLocator" class="ch.vd.dsas.rdu.transaction.jencks.TransactionManagerLocator" factory-method="getInstance">
<property name="transactionManager" ref="rduJencksTransactionManager"/>
</bean>
The application is starting and accessing data and displaying it.
However no insert / update are performed.
If I change data and submit the change the application receives the change but the data does not get flushed to the database.
I have activated logs and I see the transaction :
rdu 2015-06-18 20:28:01,817 [ http-8080-1] DEBUG [ o.s.t.j.JtaTransactionManager] Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; 'rdu'
rdu 2015-06-18 20:28:01,817 [ http-8080-1] DEBUG [ o.s.t.j.JtaTransactionManager] Participating in existing transaction
rdu 2015-06-18 20:28:01,823 [ http-8080-1] DEBUG [ o.s.t.j.JtaTransactionManager] Initiating transaction commit
But nothing is sent to the database.
If I intercept the execution through debugging and manually flush the Hibernate session, the data gets updated.
It seems the JPA / Hibernate session is not coordinated with the transaction.
I can't figure out what is missing in the configuration and why the session is not flushed automatically.
Hope you can help me with this issue.
Best regards,
Eric

The problem is due to this property:
<prop key="hibernate.transaction.jta.platform">ch.vd.dsas.rdu.ref.transaction.jencks.JencksTransactionManagerLookup</prop>
The hibernate.transaction.jta.platform property is not the same with hibernate.transaction.manager_lookup_class and it should point to an AbstractJtaPlatform implementation:
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>

Related

java app cannot connect to remote MySQL db but can connect to local MySQL db

Given two MySQL servers, one local, one remote.
Both have a database foobar containing a table bohica.
Local server has users 'myadmin'#'%' , 'myadmin'#'localhost' defined.
Remote server has users 'myadmin'#'%' , 'myadmin'#'localhost' and 'myadmin'#'my.domain.com' defined.
Privileges have been granted to all these users and privileges flushed.
Both servers are up.
From a command prompt window I can connect to both servers, ie
mysql --user=myadmin --password=mylocalpw
mysql --user=myadmin --password=myremotepw --host=my.domain.com
Both succeed, which proves that I can reach and log in to the remote server.
My java/maven/hibernate app has a context file
...
<bean id="databasePropertiesServerB"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--
<property name="location" value="classpath:databaseServerBlocal.properties" />
-->
<property name="location" value="classpath:databaseServerBliveadmin.properties" />
<property name="placeholderPrefix" value="$dbServerB{" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="dataSourceServerB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="XADBMS_B" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="databaseName">foobar</prop>
<prop key="user">$dbServerB{hibernate.connection.username}</prop>
<prop key="password">$dbServerB{hibernate.connection.password}</prop>
</props>
</property>
<property name="poolSize"><value>20</value></property>
<property name="testQuery" value="SELECT 1" />
</bean>
<bean id="emfB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.mybiz.forms" />
<property name="dataSource" ref="dataSourceServerB" />
<property name="jpaDialect" ref="jpaHibernateDialect" />
<property name="jpaVendorAdapter" ref="jpaHibernateVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">$dbServerB{hibernate.dialect}</prop>
<prop key="hibernate.connection.characterEncoding">$dbServerB{hibernate.connection.characterEncoding}</prop>
<prop key="hibernate.connection.driver_class">$dbServerB{hibernate.connection.driver_class}</prop>
<prop key="hibernate.connection.url">$dbServerB{hibernate.connection.url}</prop>
<prop key="hibernate.connection.release_mode">$dbServerB{hibernate.connection.release_mode}</prop>
<prop key="hibernate.cache.provider_class">$dbServerB{hibernate.cache.provider_class}</prop>
<prop key="hibernate.c3p0.min_size">$dbServerB{hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">$dbServerB{hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.timeout">$dbServerB{hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">$dbServerB{hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.show_sql">$dbServerB{hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">$dbServerB{hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">$dbServerB{hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
</bean>
...
and properies files
databaseServerBliveadmin.properties
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://my.domain.com:3306/foobar
hibernate.connection.username=myadmin
hibernate.connection.password=myremotepw
hibernate.connection.release_mode=after_transaction
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.c3p0.init_size=10
hibernate.c3p0.min_size=10
hibernate.c3p0.max_size=50
hibernate.c3p0.timeout=600
hibernate.c3p0.max_statements=50
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
and databaseServerBlocaladmin.properties
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/foobar
hibernate.connection.username=myadmin
hibernate.connection.password=mylocalpw
hibernate.connection.release_mode=after_transaction
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.c3p0.init_size=10
hibernate.c3p0.min_size=10
hibernate.c3p0.max_size=50
hibernate.c3p0.timeout=600
hibernate.c3p0.max_statements=50
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
Now it gets weird.
When I adjust the "location" property value in the "databasePropertiesServerB" bean to use databaseServerBlocal.properties,
the app can connect to the local server and do its thing as expected.
BUT (and you knew there a BUT coming...)
When I adjust the "location" property value in the "databasePropertiesServerB" bean to use databaseServerBliveadmin.properties,I get the dreaded
java.sql.SQLException: Access denied for user 'myadmin'#'localhost' (using password: YES)
errmsg. I can log in manually to the remote server which proves the username and password are correct.
I've been very careful to spell the username and password values correctly in the two .properties files - no trailing spaces, etc. So I'm stumped at this point. Any ideas?
TIA,
Still-learning Stev
complete answer appears here in a subsequent question
cannot achieve connectivity with MySQL db on remote machine
basically, everything about the hibernate properties file and the accounts and passwords on both instances of MySQL were all correct - the cause was not explicitly setting the xaProperties.server property in the Atomikos datasource bean that gets used by the entity manager factory bean - if not explicity set it defaults to 'localhost', which of course doesn't work on a remote machine because there it's trying to log in as xxxxx.%
Sneaky sneaky sneaky.
CASE CLOSED
Still-learning Steve

Spring + Hibernate Transaction Management using Remote JNDI Datasource

I have a Spring application, that uses hibernate, Spring transaction management and a JNDI datasource located on Weblogic server.
Application works fine when deployed on weblogic, however it throws following error when running on local as a standalone java application (without container) -
org.hibernate.service.jndi.JndiException: Error parsing JNDI name
[javax.transaction.TransactionManager]
javax.naming.NoInitialContextException: Need to specify class name in
environment or system property
Here is the transaction manager, datasource and hibernate configuration I am using in my setup:
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"></bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/mydatasource"/>
<property name="jndiEnvironment" ref="weblogicJndiEnv"></property>
</bean>
<util:properties id="weblogicJndiEnv">
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.security.principal">weblogic</prop>
<prop key="java.naming.security.credentials">weblogic</prop>
<prop key="java.naming.provider.url">t3://#{ systemProperties['listenurl'] }</prop>
</util:properties>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jtaTransactionManager" ref="transactionManager"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
<property name="packagesToScan" value="abc.xyz" />
</bean>
Is there anything I am missing in the above configuration?

Quartz scheduler - each time new jdbc connection

I have problem with Quartz Scheduler and configuration for database. Every time scheduler check if new job exist is created new JDBC connections. How to avoid create new connection ?
2015-06-19 10:42:05,522 DEBUG DriverManagerDataSource:142 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8]
2015-06-19 10:42:05,544 DEBUG LocalDataSourceJobStore:3182 - Found 0 triggers that missed their scheduled fire-time.
2015-06-19 10:42:05,545 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource
2015-06-19 10:42:07,522 DEBUG LocalDataSourceJobStore:3933 - MisfireHandler: scanning for misfires...
2015-06-19 10:42:07,522 DEBUG DriverManagerDataSource:142 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8]
2015-06-19 10:42:07,539 DEBUG LocalDataSourceJobStore:3182 - Found 0 triggers that missed their scheduled fire-time.
2015-06-19 10:42:07,539 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource
And configuration
<bean id="scheduler" name="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
scope="singleton">
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.instanceName">USER_JOBS</prop>
<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">
org.quartz.impl.jdbcjobstore.StdJDBCDelegate
</prop>
<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
<prop key="org.quartz.jobStore.isClustered">false</prop>
<prop key="org.quartz.jobStore.clusterCheckinInterval">20000</prop>
<prop key="org.quartz.jobStore.misfireThreshold">2000</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
and datasource, the same for hibernate and quartz scheduler
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
As far as I see you use org.springframework.jdbc.datasource.DriverManagerDataSource class as data source. According to the javadoc it creates jdbc connection every time someone call getConnection. I'm sure Quartz call this method internally.
To solve problem you should use pooled DataSource. For example, c3p0 (Look at com.mchange.v2.c3p0.ComboPooledDataSource)

org.hibernate.HibernateException: No TransactionManagerLookup specified on hibernate upgrade

I am running spring 4.1.4, hibernate 4.3.8, atomikos 3.9.3, java 8, tomcat 8.
I see the above exception in localhost.log when I start my server but I'm not sure where to configure the TransactionManagerLookup apart from where I am already configuring it. This wasn't happening before upgrading hibernate so it is most likely a versioning issue. Could anyone help please?
FYI: catalina.out shows nothing useful. Just:
SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Error listenerStart
SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
My appContext defines :
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
and the full stack trace is:
22-Jan-2015 10:07:25.734 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class com.my.app.web.InitializerListener
org.hibernate.HibernateException: No TransactionManagerLookup specified
at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:85)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)
at com.my.app.dao.AbstractMyDAO.criteria(AbstractMyDAO.java:86)
at com.my.app.dao.AbstractMyDAO.count(AbstractMyDAO.java:79)
at com.my.app.initialize.MyInitializerImpl.initializeApplicaiton(MyInitializerImpl.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy220.initializeApplicaiton(Unknown Source)
at com.my.app.web.MyInitializerListener.contextInitialized(MyInitializerListener.java:42)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
EDIT: The Atomikos Transaction manager is configured as such
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true" />
<!-- in secs -->
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<!-- in secs -->
<property name="transactionTimeout" value="300" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION" />
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
EDIT 2:
I think I need to clarify things a bit here.
I have a DAO object called AbstractMyDAO (as you can see from the stack). In this object is defined a session factory
#Autowired
private SessionFactory mySessionFactory;
when currentSession is called the above error is thrown because hibernate cannot find the transaction manager lookup associated with this session factory.
public Session currentSession() {
return mySessionFactory.getCurrentSession();
}
This session factory is defined in my app context file as such:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>some values... </value>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${gst.hibernate.dialect}
</prop>
<prop key="query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.show_sql">${my.hibernate.showsql}</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">${ehcache.my.persist.config}</prop>
<prop key="hibernate.connection.isolation">3</prop>
<prop key="connection.release_mode">auto</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/log/my/lucene/indexes</prop>
</props>
</property>
</bean>
As you can see, the property hibernate.transaction.manager_lookup_class is being defined but can't be found when the bean is created and I have no idea why. Has the configuration changed?
EDIT 3:
When I debug:
final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService( JtaPlatform.class );
It returns a NoJTAPlatform. I guess this is my problem.
EDIT 4:
There doesn't seem to be anything that implements JtaPlatform that is suitable for tomcat or am I being mental?
Make sure you also configure the Atomikos Transaction Manager too, so the TransactionManagerLookup can locate the UserTransaction accordingly:
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
Then, you should use Spring EntityManager factory too:
<bean id="entityManager"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceXmlLocation="**/persistence.xml"
p:persistenceUnitName="persistenceUnit"
depends-on="dataSource, transactionManager">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
...
</props>
</property>
</bean>
Then the DAOs should inject the EntityManager:
#PersistenceContext(unitName = "persistenceUnit")
private EntityManager entityManager;
Instead of calling currentSession(AbstractMyDAO) directly:
at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)
The answer to my question is here.
"In Hibernate 4.3 the long deprecated TransactionManagerLookup got removed. Now the JTA provider must implement org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform."
I Solved it by
into the hibernate.cfg.xml file
changing the
<property name="hibernate.current_session_context_class">thread</property>
to thread instead of jta .
More details here

Hibernate doesn't start

I have the following hibernate config on spring and the server starts after a long time but doesn't connect to DB (no schema on DB). So It was supose to give a error message or create the schema with <prop key="hibernate.hbm2ddl.auto">create</prop>.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method = "close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://dburl:3306"/>
<property name="user" value="user"/>
<property name="password" value="pass!"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>waf/resources/User.hbm.xml</value>
<value>waf/resources/Post.hbm.xml</value>
<value>waf/resources/Position.hbm.xml</value>
<value>waf/resources/Comment.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<!-- C3P0 CONNECTION POOL -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">20</prop>
<prop key="c3p0.max_statements">50</prop>
<prop key="c3p0.min_size">1</prop>
<prop key="c3p0.timeout">10</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
Can you guys help me out?
Hibernate does not create schemas with hbm2ddl.auto. It just creates | creates-drop | etc tables.
Going through the HBM files, you have given as below..
Validate instead of Create
I hope this will not create the DDL. can you please check that? Or is it a typo in question?
Adding to what others have proposed if you are using connection pool (which you are as c3po is mentioned) then while the session Factory is created it will try to use the connection pool backed datasource which in turn will connect to database to pre-create and pool connection. When you say that it does not connect to database - how do you know that ? Is there error in logs? I have seen that if Hibernate session factory is not able to configure itself it throws an error in the logs.

Categories