Deploying on jboss using getCurrentSession - java

I have implemented DAO's based on Link in the HibernateDAOFactory class, there is a method
protected Session getCurrentSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}
To implement the same i have made the hibernate.cfg.xml changes as described in the below articles Here 1
HERE 2
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property><br/>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.current_session_context_class">thread</property>
While deploying it on jboss i get the following errors
org.hibernate.TransactionException: could not register synchronization
I even set the property
<property name="hibernate.current_session_context_class">jta</property>
but i got the following error.
Caused by: org.hibernate.HibernateException: Unable to locate current JTA transaction
at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:88)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
Am i missing something out here??
My jboss version is 4.2.3 and hibernate version is 3.3.1 and using Java 7.

Well for thread based session management I removed the transaction.factory_class and manager_lookup_class property from my hibernate.cfg.xml and it worked like a charm.
But for jta based session management, I am still getting the same error.

Related

JPA; Setting 'transaction-type' on annotation

I'm using Eclipselink's implementation of JPA and this is how I'm instantiating persistence context:
#PicketLink
#PersistenceContext(unitName = "txPersistUnit.security")
private EntityManager txEmSec;
this is persistence unit defitnition:
<persistence-unit name="txPersistUnit.security" transaction-type="RESOURCE_LOCAL">
...
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL"/>
<property name="eclipselink.cache.shared.default" value="true"/>
...
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode"
value="database"/>
</properties>
</persistence-unit>
so, you can see I am setting RESOURCE_LOCAL as a transaction-type but I'm getting this error when deploying:
java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while preparing the app : The persistence-context-ref-name [com.txsolutions.manager.PersistenceManager/txEmSec] in module [txAPI] resolves to a persistence unit called [txPersistUnit.security] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.. Please see server.log for more details.
Server is Glassfish 4.0.1
Question is why is glassfish not deploying this application succesfully when transaction-type set to RESOURCE_LOCAL?
I'm emphasizing that I have RESOURCE_LOCAL persistence unit in that same application on that same server deployed.
Now, when I create entity manager like this:
..declarations omitted..
factory = Persistence.createEntityManagerFactory("txPersistUnit.security");
entityManager = factory.createEntityManager();
it is created sucessfuly even with RESOURCE_LOCAL as for transaction type.
So all in all whats the difference between this two approaches?
Thanks!
Since you are running your code in an JEE compliant Application Server (i.e. Glassfish), your transaction type should be JTA.
<persistence-unit name="txPersistUnit.security" transaction-type="JTA">
RESOURCE_LOCAL is generally used for Standalone Java SE applications.
Since you are using #PersistenceContext, means that you are using a Container-Managed entity manager/persistence context. Since it is container-managed, it requires you to set your transaction type to JTA.
I suggest you try using an Application-Managed persistence context. Use #PersistenceUnit to inject an instance of EntityManagerFactory, then from the factory create the entity manager. Example below.
#PersistenceUnit(unitName="txPersistUnit.security")
EntityManagerFactory emf;
....
// somewhere in your code
EntityManager em = emf.createEntityManager();

'hibernate.dialect' must be set when no Connection available error

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.

Using JOTM transaction manager Doesn't works on Tomcat

Facing a strange problem here, below is the configuration i am using:
1. Apache Tomcat 6.0.26
2. mySql
3. Spring framework+ Hibernate
We have used JOTM for transaction management
<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
< property name="userTransaction" ref="jotm" />
</bean>
The problem is that i have one service which creates and entity on my local DB and then calls a web service on failiure of which the created entity in local DB should be rolled back. On failure of the web service call i am throwing RunTimeException which should ideally rollback the transaction as per mentioned in the spring configuration file. However this is not happening. Instead of JOTM if i am using Hibernate transaction manager it is rolling back the transaction. Can someone please throw some light on if i am missing out on anything while implementing through JOTM.
Appreciate the help in advance,
Vaibhav

How to enable custom isolation levels for a JTA Transaction Manager in Spring

Question
How do I configure a JtaTransactionManager object with allowCustomIsolationLevels set to true via Spring such that the Spring configuration can be used across multiple application servers?
Background:
I have an application that is currently running out of JBossAS and I'm trying to get it to run in WebSphere. The only issue I'm currently having is getting the correct JTA Transaction Manager injected with the proper settings.
Here's the old setting
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName">
<value>java:/TransactionManager</value>
</property>
<property name="allowCustomIsolationLevels" value="true" />
</bean>
This worked since JBossAS has it's JTA Transaction Manager defined at JNDI location java:/TransactionManager. However, WebSphere does not have the same JNDI location.
Spring 2.5.x provides a way to get the JTA Transaction Manager in a generalized way.
<tx:jta-transaction-manager />
This gets the JtaTransactionManager object and defines it as a bean with the id transactionManager.
I looked in the Spring TX schema, but the only setting available is to set a specific isolation level, but not just to allow custom levels to be used (as defined elsewhere). How do I set the allowCustomIsolationLevels property using the tx:jta-transaction-manager tag?
Transaction Managers and Websphere:
Websphere does not use the typical jndi standard when supplying the transaction manager. Spring has worked around this by providing the org.springframework.transaction.jta.WebSphereUowTransactionManager that you can use to lookup the websphere transaction manager.
Datasource and Isolation Levels
You typically cannot change the isolation level of a datasource and I know you cannot change it when connecting from websphere to DB2 database (it's set as a parameter on the datasource configuration). The allowCustomIsolationLevels flag lets you select different data sources for different requested isolation levels..
See here and here

Hibernate MySQL transaction configuration issue

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>

Categories

Resources