I'm having trouble starting a transaction with Hibernate and MySQL while running in JUnit. I'm getting a HibernateException which states: "No TransactionManagerLookup specified". I believe this error is because I don't have a proper configuration setting for hibernate.transaction.manager_lookup_class.
I see that under the namespace of org.hibernate.transaction there are quite a few different lookup classes that I could use. All of the documentation that I could find on these was very vague. My question is what is the appropriate one for MySQL?
I do it with Spring and its transaction managers. Works perfectly.
To fix this I needed to make the following changes.
Changed the hibernate.cfg.xml => hibernate.current_session_context_class from jta to thread.
Changed the transaction manager to
org.springframework.orm.hibernate3.HibernateTransactionManager
in the bean configuration.
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" >
<property>
<bean>
Related
We have an application developed using Spring 3.1.1 and Hibernate 3.6.10, and uses Oracle 11g as database.
The application looks-up the datasource using JNDI, and uses a org.springframework.orm.hibernate3.HibernateTransactionManager.
And spring configuration file contains:
<jee:jndi-lookup jndi-name="jdbc/ds"
id="dataSource" proxy-interface="javax.sql.DataSource"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
....
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
We use Tomcat in development machines, but the application is deployed on Weblogic 11 for production.
The problem is in Weblogic datasource.log file, the following message is logged:
<autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=false,doInit=false,'null',destroyed=false,poolname=life_rac,appname=null,moduleName=null,connectTime=45,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1531636426817,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:356)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:364)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:330)
...
Now we are worried about that autoCommit=true in the log messages, cause it seems that in some cases the first DML queries are committed when they shall not, especially when the Weblogic is stopped due to some problems (for example power-loss).
I shall mention that the data-source defined in Weblogic Server is a XA-Datasource.
Is there a way to change default configuration of autoCommit to false?
As we have just one database, and no other transactional resources, I believe we change can the datasource to a non-XA one, without any impact. Am I correct?
And we need to change the configuration so it works both on Weblogic and Tomcat (adding some configuration to Tomcat -such as adding a transaction manager- is okay).
WebLogic Server 11g allows you to create and deploy a jdbc interceptor class. This documentation will help to write the class. Each time a connection is created, your interceptor will be called and you will be able to set the auto commit property to false.
This way is deprecated in 11g. 12c uses a Connection Initialization Callback.
I am getting the following error when using Hibernate:
'hibernate.dialect' must be set when no Connection available
And I am using a datasource for database connection.
The issue could be that you haven't installed the client library for the database you are trying to connect to.
I have a Spring application that does not have a persistence.xml file and therefore no hibernate.dialect declaration.
Once I installed the MySQL Connector/J client library the error went away.
EDIT: I've also gotten this error when the database server wasn't running. Something that often happens now that I run my MySQL server through MAMP.
You will get this issue even if you have your configuration files having proper value but you don't configure it in the code.
I was explaining hibernate, I forgot to use configure() before buildSessionFactory() so I was getting the error.
You might want to recheck it.
Previous code which was giving me error
SessionFactory factory = new Configuration().buildSessionFactory();
Changed code No Error
SessionFactory factory = new Configuration().configure().buildSessionFactory();
This error is hibernate doing a poor job of telling you what went wrong with your attempted connection with database.
In my case it was as simple as having a wrong password in config file.
You need to set the property
hibernate.dialect
In the hibernate (persistence.xml or bean declaration) configuration, the value depends on your database, for example:
Postgres: org.hibernate.dialect.PostgreSQL82Dialect
Oracle: org.hibernate.dialect.Oracle10gDialect
All posible options are listen here
For example, a sample persistence.xml looks like:
<persistence-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
Just encountered this issue. In my case it was the hibernate.dialect configuration.I added the following to SessionFatcory config in spring context file:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.testapp.service.geolocation.LocationData</value>
<value>com.testapp.service.profiles.Profile</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
I had this problem too. The reason was missing <property name="dataSource" ref="dataSource" /> element in <bean id="sessionFactory"> definition.
In some cases just using a wrong name for the database results in this Exception. Hibernate is apparently trying to determine the dialect before doing anything else, and as the DB cannot be reached, the error message comes from the part responsible for the dialect select. Bad error messaging on part of Hibernate.
In the Hibernate configuration dialog has a tab "options" it is possible to select some.
In this case I was using Oracle Enterprise Pack for Eclipse that already had configured connector, but was still getting the error. Select an option on the list was enough to solve.
I had the same errors.
My problem was that I put the hibernate.properties under one package instead of the src.
So my solution to my problem was moving hibernate.properties from package to src.
I am using Jackrabbit to store files (data store) and Hibernate almost everything else. I do not know alot of transactions etc. but I know that I want a global transaction for these two so that an exception rolls back everything. At the moment the database stuff is rolled back, but Jackrabbit isn't.
I am using Spring 3.2. I have deployed the Jackrabbit JCA adapter to JBoss (7.1.1). I have these lines in the config xml:
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager"></bean>
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true"/>
However that isn't enough it seems. Could somebody give me some clues to what I need to read about/where to find information on this? I need to roll back everything if something happens to one of the resource providers.
This was quite tricky to set up. One gotcha was the spring automagically uses 'transactionManager' as a default name which caused problems. It is definitely worth the effort though as synchronising mismatched data sources would soon become a nightmare.
Here is my sample config that worked for me. Of course now we would probably use #Configuration instead of the xml. This worked using, Spring Data, JTA, MySQL with XA driver and Neo4j. Neo4j specific things have been omitted.
Spring Data may also help you here.
Here is a starter...
<tx:annotation-driven transaction-manager="xaTransactionManager" />
<bean id="xaTransactionManager" class="some.type.of.ChainedTransactionManager">
<constructor-arg>
<list>
<ref bean="jpaTransactionManager"/>
<ref bean="otherTransactionManager"/>
</list>
</constructor-arg>
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="localContainerEntityManagerFactoryBean"/>
</bean>
<bean id="otherTransactionManager" class="other.type.of.jta.TransactionManager">
<property name="transactionManager" ref="otherTransactionManagerService"/>
</bean>
Does any one know, how to configure cache for hibernate with jboss ?
My clear question is I am using JPA and Jboss. Every time I call JPA method its creating entity and binding query.
My persistence properties are
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
And I am creating entity manager the way shown below:
emf = Persistence.createEntityManagerFactory("pu");
em = emf.createEntityManager();
em = Persistence.createEntityManagerFactory("pu")
.createEntityManager();
Is there any nice way to manage entity manager resource insted create new every time or any property can set in persistance. Remember it's JPA.
The question is not clear, there are many second level cache providers for Hibernate and they are not application server specific.
To enable the second level cache, you need to set the following properties in Hibernate configuration file hibernate.cfg.xml:
<property name="hibernate.cache.use_second_level_cache">true</property>
And if you want to also enable query result caching:
<property name="hibernate.cache.use_query_cache">true</property>
Then, declare the name of a class that implements org.hibernate.cache.CacheProvider - a cache provider - under the hibernate.cache.provider_class property. For example, to use JBoss Cache 2:
<property name="hibernate.cache.provider_class">org.hibernate.cache.jbc2.JBossCacheRegionFactory</property>
Of course, the JAR for the provider must be added to the application classpath.
That's for the Hibernate side. Depending on the chosen cache provider, there might be additional configuration steps. But as I said, there are many second level cache providers: EHCache, JBoss Cache, Infinispan, Hazelcast, Coherence, GigaSpace, etc.
I read about using
<context:component-scan base-package="tld.mydomain.business">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
and annotate my service beans with #Service("myService"), and thought great, I'll do that, since I'm already doing that with my controllers. My usual service bean configuration looks like
<bean id="userService" parent="txProxyTemplate">
<property name="target">
<bean class="tld.mydomain.business.UserServiceImpl"/>
</property>
<property name="proxyInterfaces" value="tld.mydomain.business.UserService"/>
</bean>
so now that I generate them, how do I wrap them in a Hibernate proxy such as TransactionProxyFactoryBean? Or is there a better way to do that as well?
I have not yet gone all the way and used #Repository as well, is that required?
Cheers
Nik
Using TransactionProxyFactoryBean is not encouraged in modern Spring applications, although it still works. The typical approach nowadays is to annotate classes with #Transactional, and then stick this element in your application context file:
<tx:annotation-driven transaction-manager="txManager"/>
This and other strategies are discussed in great depth in the reference document, and there's even a side note about TransactionProxyFactoryBean.
There's no need for
<context:include-filter type="annotation"expression="org.springframework.stereotype.Service"/>
Spring will register #Service, #Repository, #Component... once they are found in the base package.
Like #Rob said either use #Transactional or <aop:config>...</aop:config> to handle your transactions at the service level.
If you have two different resources that need to be in the same transaction, then you will need to use JTA. See my answer to an earlier question here. Your config would need to look something like:
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="appserver/jndi/path" />
</bean>
Where appserver/jndi/path would need to be replaced with the JNDI path of the JTA transaction manager that comes with your application server (although you can use a standalone JTA transaction manager such as JOTM as well). Typical paths as mentioned in the 2.5.x API are:
"java:comp/UserTransaction" for Resin 2.x, Oracle OC4J (Orion), JOnAS (JOTM), BEA WebLogic
"java:comp/TransactionManager" for Resin 3.x
"java:appserver/TransactionManager" for GlassFish
"java:pm/TransactionManager" for Borland Enterprise Server and Sun Application Server (Sun ONE 7 and later)
"java:/TransactionManager" for JBoss Application Server