org.hibernate.TransactionException: Unable to locate JTA UserTransaction - java

I am writing my first example for Inserting Record using Hibernate Framework .
To start with I am trying to following instructions given at URL
You can see my code at GIT Hibernate Basic
When calling below piece of code
public Long saveEmployee(Employee emp){
Session session = new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Long id = (Long) session.save(emp);
session.getTransaction().commit();
session.close();
return id;
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- validate | update | create | create-drop -->
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
<property name="hbm2ddl.auto">validate</property>
<!-- added as suggested in below answer-->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
It throws org.hibernate.TransactionException: Unable to locate JTA UserTransaction
Details :
I am not deploying the application from any Application server I am running it as plan Java application.
I am using Hibernate 4.1
Is it neccessary to deploy it in Application Server ?
Whether we have to explicitly specify the JTAUsertransaction in Hibernate 4.1 version?
=============== Update ==============
With
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program .
But I still wanted to know the reason why its not working when using org.hibernate.transaction.JTATransactionFactory

When used
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program.
I have deliberately posted it as an answer so it would be helpful for someone.

You would be missing the JTA configuration in your hibernate config file
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

Related

Hibernate's createSQLQuery won't retreive data

Hibernate won't retrieve data even though the data exists in the table. Running the query directly from SQL Server Management Studio shows the expected results. Any idea what am I missing?
Java:
List<User> userCollection= session.createSQLQuery("select column1, column2 from myTable").addEntity(User.class).list();
Hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name = "hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name = "hibernate.connection.url">jdbc:sqlserver://server:1433;databaseName=MyDatabase</property>
<property name = "hibernate.connection.username">user</property>
<property name = "hibernate.connection.password">123</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">validate</property> -->
<!-- Names the annotated entity class -->
<mapping class="com.app.User"/>
</session-factory>
</hibernate-configuration>

Could not load com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask. java.lang.IllegalStateException

I am working with a Mobile web application, where for the database part i am using Hibernate and for the connection pooling c3p0, when i am running the application in the beginning its working fine, but after did some transaction like select, save, update, i am getting the following exception, i don't know why its happening,
Sep 22, 2015 12:40:06 PM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already. Could not load com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1562)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040)
at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507)
at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477)
at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565)
at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:2089)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Exception in thread "C3P0PooledConnectionPoolManager[identityToken->2sa3m19b1vnwmg61s8v1zi|5363e54]-AdminTaskTimer" java.lang.NoClassDefFoundError: com/mchange/v2/resourcepool/BasicResourcePool$1DestroyResourceTask
at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040)
at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507)
at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477)
at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565)
at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:2089)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
... 8 more
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">admin</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/db</property>
<!-- SQL dialect -->
<property name="dialect">com.bss.util.PostgreSQLDialectCustom</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<mapping class="com.bss.model.TestUser"/>
<mapping class="com.bss.model.ItappFriendlist"/>
<mapping class="com.bss.model.ItappFriendlistPK"/>
<mapping class="com.bss.model.User"/>
<mapping class="com.bss.model.UserSession"/>
</session-factory>
</hibernate-configuration>
So, you are hitting tomcat hot-redeploy ClassLoader issues.
There are two things you should do:
Make sure that when your application is shutdown, your Hibernate SessionFactory gets close()ed as well. The best place to do this is a ServletContextListener. Otherwise, on hot redeploy, c3p0 Threads from a now-discarded app will continue to be running. See SessionFactory.close()
Try the settings described here to prevent stray references to objects from a defunct ClassLoader. You can just add
<property name="hibernate.c3p0.privilegeSpawnedThreads">true</property>
<property name="hibernate.c3p0.contextClassLoaderSource">library</property>
to your c3p0 config section.
(p.s. Be sure you are using a recent version of c3p0. These settings are new-ish.)

Hsql embedded swing app not run as executabe jar

Hi i created a Hibernate powered hsql embedded swing app..When i run eclipse juno version it run perfectly..After that i export as runnable jar file as run it.But does not retrieve,add data database...!!!
Project directory like
Project
..src
.....dao
.....daoimpl
.....sevice
.....serviceimpl
.....domain
.....main
.....hibernate.cfg.xml
..db/hsql/library
.............library.script
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:db/hsql/library;shutdown=true</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- JDBC connection pool (use the built-in one) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- disable batching so HSQLDB will propagate errors correctly. -->
<property name="jdbc.batch_size">0</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- List all the mapping documents we're using -->
<mapping class="com.habitz.librarymanagement.domain.Admin" />
<mapping class="com.habitz.librarymanagement.domain.Book" />
<mapping class="com.habitz.librarymanagement.domain.Category" />
<mapping class="com.habitz.librarymanagement.domain.Group" />
<mapping class="com.habitz.librarymanagement.domain.Issue" />
<mapping class="com.habitz.librarymanagement.domain.Member" />
</session-factory>
</hibernate-configuration>
Someone know please help...!!
This property creates a database relative to where the jar was executed from.
<property name="hibernate.connection.url">jdbc:hsqldb:db/hsql/library;shutdown=true</property>
Relative URL's do not work. Use an absolute URL. For example, you can use the user's directory for the database files, or a directory that your app is installed in.

Hibernate does not see data inserted from external application

I am using Hibernate as an ORM for a server daemon.
This code returns no items if the table associated is empty at run time but has an item added outside of hibernate.
public static List<QueueItem> queue()
{
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.flush();
session.clear();
#SuppressWarnings("unchecked")
List<QueueItem> topList = session.createQuery("FROM QueueItem i ORDER BY i.id asc").setMaxResults(3).list();
session.close();
return topList;
}
Here is the hibernate config
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://10.10.1.4/lanjukebox_test</property>
<property name="hibernate.connection.username">ussqldev</property>
<property name="hibernate.connection.password">ussqldev</property>
<property name="hibernate.jdbc.batch_size">25</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">validate</property>
<!-- List of XML mapping files -->
<mapping resource="usagisoft/lan/jukebox/domain/entities/Song.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/Id3.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/QueueItem.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/Vote.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If I create an object and persist it from within the application it works fine, If there is already data in the table and I insert the data it finds the other objects, the issue arises as soon as the table is empty during run time.
This is a big issue because another application handles the inserting of data for these objects.
I am using hibernate 4.2.7.Final with Java 1.7(jdk1.7.0_45) in eclipse.
You can resolve this problem since
hibernate.cfg.xml
adding this tag:
<property name="hibernate.connection.autocommit">true</property>

Hibernate 4, EnversIntegrator could not be instantiated

I'm trying to set up Hibernate, and since 4.0.0 just came out I naturally decided to go with that. It seems that no matter what way I try to create a SessionFactory, it always leads to the same error:
Initial SessionFactory creation failed.java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.envers.event.EnversIntegrator could not be instantiated: java.lang.ClassCastException: Cannot cast org.hibernate.envers.event.EnversIntegrator to org.hibernate.integrator.spi.Integrator
It seems like there is something wrong with my Hibernate configuration but I can't figure out what. Here's my hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://192.168.0.17:5432/mydb</property>
<property name="connection.username">myusrname</property>
<property name="connection.password">mypasswd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- c3p0 configuration -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Does anyone spot anything out of the ordinary, or do you have other ideas?
I found no way to get Hibernate 4.0.0 CR4 working. Switching to 3.6.7 resolved the problem and works just fine. I conclude that there must be some bug in the release, and will file a bug report.

Categories

Resources