how to get rid of c3p0 deadlock error - java

I'm using hibernate 4.3.6 with c3p0 0.9.2.1 and I'm getting the following error very frequently:
ThreadPoolAsynchronousRunner:778 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#9abc33e -- Running DeadlockDetector[Exiting. No pending tasks.]
This slows down the server and I have no idea what's causing it and how to get rig of it.
This is my hibernate.config file:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://ip:port/app?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.isolation">2</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</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="server.c1"/>
<mapping class="server.c2"/>
<mapping class="server.c3"/>
<mapping class="server.c4"/>
<mapping class="server.c5"/>
</session-factory>
</hibernate-configuration>
In addition I also have a c3p0.properties file:
c3p0.testConnectionOnCheckout=true
c3p0.privilegeSpawnedThreads=true
c3p0.contextClassLoaderSource=library
There are a lot of questions about it but I couldn't find a concrete solution.

I had a similar problem with c3p0. My final solution was use this connection pool https://github.com/brettwooldridge/HikariCP

If you are still using c3p0, the following steps are likely to resolve this issue:
Update to the latest c3p0 (now 0.9.5.2).
Set the following config parameter:
c3p0.statementCacheNumDeferredCloseThreads=1
See the docs.
Update: Oh, wait. I responded too quickly. Sorry!
You are not actually seeing deadlocks, you are just seeing messages that the deadlock detector is running. The message that you are seeing is not an error at all, just a notification that gets logged at TRACE / DEBUG / FINEST (depending which logging library you use).
You should configure your logging so that loggers prefixed com.mchange log at INFO. That's it.

Related

Datasource configuring in Hibernate 5, Tomcat 8

Need some clarification and helps. Especially appreciate describing general concepts or link where they are described.
So, on the hibernate website I have read the next one:
For use inside an application server, you should almost always
configure Hibernate to obtain connections from an application server
javax.sql.Datasource registered in JNDI. You will need to set at least
one of the following properties:
And I have a few question because at the moment I am really confused about all of that stuff with DataSource, DataDriver, Tomcat and Hibernate in general.
Does configuring Datasource and binding SessionFactory to the JNDI
is the same process?
If no, for what we use DataSource and for why we need to bind SessionFactory to JNDI (in general)?
Am I understood right? If we configure DataSource in hibernate.cfg.xml file we don't need to configure it in {tomcat}/conf/server.xml or {tomcat}/conf/context.xml?
What is hibernate.jndi.url? Does it is the same as hibernate.connection.url?
What is hibernate.connection.datasource? In docs I read that it is "datasource JNDI name", so if I understood right it can be any name?
From Hibernate docs I read that setting at least one of the properties hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password makes my app use javax.sql.Datasource registered in JNDI. So does the next conf already configured to use DataSource?
How to check that DataSource used and configured fine?
My hibernate.cfg.xml file:
<?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>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<!--http://stackoverflow.com/questions/2067526/hibernate-connection-pool-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--For use inside an application server, you should almost always configure Hibernate to obtain connections from an application server javax.sql.Datasource registered in JNDI. You will need to set at least one of the following properties:-->
<!--hibernate.connection.datasource,hibernate.jndi.url,hibernate.jndi.class,hibernate.connection.username,hibernate.connection.password-->
<!--Datasource config-->
<property name="hibernate.connection.datasource">jdbc:mysql://localhost/easywordweb</property>
<!--<property name="hibernate.jndi.url">??????? what is it</property>-->
<!--/Datasource config-->
<!--*****************************************************************-->
<!--C3P0 config-->
<!--Hibernate will obtain and pool connections using java.sql.DriverManager if you set the 5 following properties -->
<!--hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password,hibernate.connection.pool_size-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/easywordweb</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!--We can use a third party pool for best performance and stability, for example c3p0. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0. -->
<!--<property name="hibernate.connection.pool_size">140</property>-->
<property name="hibernate.c3p0.max_size">140</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<!--max to cache-->
<property name="hibernate.c3p0.max_statements">50</property>
<!--The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Hibernate default: 0-->
<property name="hibernate.c3p0.timeout">21600</property>
<!--for test, change futher-->
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
<!--at every connection checkin to verify that the connection is valid-->
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!--at every connection checkout to verify that the connection is valid-->
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<!--/for test, change futher-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--/C3P0 config-->
<!--*****************************************************************-->
<property name="hibernate.c3p0.validate">true</property>
<!--c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds-->
<property name="hibernate.c3p0.idle_test_period">21000</property>
<property name="hibernate.jdbc.batch_size">20</property>
<!--Number rows to be returned if no setted-->
<property name="hibernate.jdbc.fetch_size">20</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!--FIXING: Table "...".hibernate_sequence table not found.-->
<property name="hibernate.id.new_generator_mappings">false</property>
</session-factory>
</hibernate-configuration>
Thank's everyone in advance.
In the config you have posted you are initializing the connection pool within your application.
An alternative is to delegate the creation of the database pool to your app/web server and expose it as a JNDI resource. Your application need then only specify the name of the JNDI datasource to obtain a connection.
Doing this in Tomcat is documented here:
https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
Your hibernate.cfg.xml then looks like:
<?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>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- The Server configured JNDI datasource -->
<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>
<property name="hibernate.jdbc.batch_size">20</property>
<!--Number rows to be returned if no setted-->
<property name="hibernate.jdbc.fetch_size">20</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!--FIXING: Table "...".hibernate_sequence table not found.-->
<property name="hibernate.id.new_generator_mappings">false</property>
</session-factory>
</hibernate-configuration>

mysql utf-8 returns gibberish from one machine

I have mysql DB with UTF-8 and application written in java with hibernate. When I run the application in eclipse everything is fine. But in production which is on a different machine the values returned from DB are corrupted.
I print the values to log (immediately after getting them) and I clearly see that the values I get from DB are corrupted in production.
The DB itself is the same DB for both environments. The values are stored fine.
Any ideas what can be the reason for this?
UPDATE:
I forgot to say that it happens sometimes. I think that in 50% of the times it works fine.
UPDATE 2:
Here is the hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://myDB:3306/myApp?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!-- <property name="hibernate.connection.pool_size">10</property> -->
<property name="hibernate.connection.isolation">2</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider</property>
<property name="hibernate.hikari.minimumIdle">5</property>
<property name="hibernate.hikari.maximumPoolSize">10</property>
<property name="hibernate.hikari.idleTimeout">30000</property>
<mapping class="someclass1"/>
<mapping class="someclass2"/>
<mapping class="someclass3"/>
<mapping class="someclass4"/>
</session-factory>
</hibernate-configuration>
Reference URL: https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html
Please verify the datasource configuration in your production environment whether the attributes - useUniCode and characterEncoding are set properly. Example:
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/exodb?relaxAutoCommit=true&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>

Error creating Hibernate Mapping Files and PJOS from Database

I use java version 8.0.2 and Mysql Gui 9:02 software.I try to create Hibernate Mapping Files and PJOS from Database but I always get the following error message
Hibernate configuration fails with message:Could not get list of suggested identity strategies from database. Probably a JDBC driver problem. see message.log for exception details.
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>
<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://localhost:3306/mind_1.0.2?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Please Help Me!
I think you missed the db password tag.
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/DBname
</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
Maybe your database name is not right.
And try to add
<property name="hibernate.default_schema">dbo</property>

Hibernate auto create database

I have a Java EE Hibernate project, and I'm using MySQL as a database.
I want that when I first time run the project, it will create the database automatically.
This is my hibernate.cnf.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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="entities.Business" />
<mapping class="entities.Coupon" />
<mapping class="entities.User" />
<mapping class="entities.LastLogin" />
</session-factory>
</hibernate-configuration>
When I first time run this project on another computer, how can I make the database InternetProject to be created?
According to the config file, it might already do it and I'm not aware to it.
Thanks in advance.
<property name="hibernate.hbm2ddl.auto">create</property>
will create tables. But it will not create database. Change the connection url to generate the database.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
Update : character encoding when creating database
<property name="connection.url">
jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>
<property name="hibernate.hbm2ddl.auto">create</property>
will do
Hibernate will not create the database for you, only the tables. To create the database you need to tell MySQL to create it if it does'nt already exist by adding a parameter to the URL. E.g.:
jdbc:mysql://db:3306/mydatabase?createDatabaseIfNotExist=true
There are multiple option for auto property.
create - It creates new tables corresponding mapping or annotation. It drops existing tables and data.
update - It keeps existing data and tables. It updates schema. here we have to take care contrants.
create-drop - It is same like create but once session gets closed it drops everything.
validate - it validates or matches schema with map or annotation. It's valid for Production environment.
<!-- create create-drop validate update -->
<property name="hbm2ddl.auto">update</property>
I hope it helps.

org.hibernate.TransactionException: JDBC rollback failed exception

I'm using Tomcat server, I started it yesterday morning and it continuously runs until this morning, but when I tried to login in my application it shows:
org.hibernate.TransactionException: JDBC rollback failed
What is the problem - is any persistent session here or is sessionfactory null?
I got the problem ,
ie i attached a different jar named c3p0-0.9.1.jar file and add some lines in hibernate-cfg.xml file
The code is :
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/<your db name></property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.jdbc.batch_size">50</property>
//Here is the extra code for handling the above problem.....
**<property name="hibernate.c3p0.max_size">1</property>
<property name="hibernate.c3p0.min_size">0</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">1000</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>**
</session-factory>
</hibernate-configuration>

Categories