I use JBOSS 7.1 web server for my application. I configured JBOSS connection pool and use it for JDBC connections. I also use hibernate in my application. And i want to know if hibernate also use this connection pool or me need to configure other connection pool(for example c3p0) for hibernate?
C3P0 Connection Pool
<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>
hibernate.c3p0.min_size:
Minimum number of JDBC connections in the pool.
Hibernate default: 1
hibernate.c3p0.max_size:
Maximum number of JDBC connections in the pool.
Hibernate default: 100
hibernate.c3p0.timeout:
When an idle connection is removed from the pool (in second).
Hibernate default: 0, never expire.
hibernate.c3p0.max_statements:
Number of prepared statements will be cached. Increase performance.
Hibernate default: 0, caching is disable.
hibernate.c3p0.idle_test_period:
idle time in seconds before a connection is automatically validated.
Hibernate default: 0
http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch01.html
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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:schema</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">schema</property>
<property name="show_sql">true</property>
<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>
hibernate.c3p0.min_size – Minimum number of JDBC connections in the pool. Hibernate default: 1
hibernate.c3p0.max_size – Maximum number of JDBC connections in the pool. Hibernate default: 100
hibernate.c3p0.timeout – When an idle connection is removed from the pool (in second). Hibernate default: 0, never expire.
hibernate.c3p0.max_statements – Number of prepared statements will be cached. Increase performance. Hibernate default: 0 , caching is disable.
hibernate.c3p0.idle_test_period – idle time in seconds before a connection is automatically validated. Hibernate default: 0
more detail refer link
Related
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>
In my web app I use the session-per-request pattern. I have a connection pool and open sessions in a Filter using SessionFactory.openSession() and then Session.close().
In the same app, for a complex process, I want to use session-per-conversation. I tried to open a second Session with SessionFactory.openSession(), but subsequently calling Session.disconnect() does nothing.
How can I manually connect/disconnect sessions? This is my Hibernate configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/.....</property>
<property name="hibernate.connection.username">...</property>
<property name="hibernate.connection.password">...</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.test_connection_on_checkout">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
</session-factory>
</hibernate-configuration>
The Java code:
longSession = mySessionFactory.openSession();
System.out.println(((SessionImplementor) longSession).isConnected());
longSession.disconnect();
System.out.println(((SessionImplementor) longSession).isConnected());
This outputs true twice...
I think this is related to this note in Hibernate Documents:
Note that disconnect() called on a session where the connection was
retrieved by Hibernate through its configured ConnectionProvider has
no effect, provided ConnectionReleaseMode.ON_CLOSE is not in effect
Also note to this in Documentation for session-per-conversation:
Committing a database transaction disconnects a session from the JDBC
connection and returns the connection to the pool
This say you need only to commit transactions to return the connection to the pool, except for the last transaction in your conversation:
// foo is an instance loaded earlier by the old session
Transaction t = session.beginTransaction(); // Obtain a new JDBC connection, start transaction
foo.setProperty("bar");
session.flush(); // Only for last transaction in conversation
t.commit(); // Also return JDBC connection
session.close(); // Only for last transaction in conversation
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.
I am change my hibernate.cfg.xml added new options:
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="show_sql">true</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/pirates</property>
<property name="connection.username">postgres</property>
<property name="connection.password">mmm888</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">200</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>
<property name="hibernate.generate_statistics">true</property>
and added hibernate-c3p0-4.1.4.Final.yar in my project, but I'm not sure that I'm using c3p0. I can set hibernate.c3p0.max_size = 2 but Hibernate continues to create 400 ( 400 -for example) threads - if it needs to.
how to check that I am using c3p0 in hibernate ?
You are missing this line:
<property name="connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider"/>
This tells Hibernate to use the C3P0 connection pool.
Not sure the above answer is necessary, according to C3P0ConnectionProvider document:
https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/connection/C3P0ConnectionProvider.html
It says:
"A connection provider that uses a C3P0 connection pool. Hibernate will use this by default if the hibernate.c3p0.* properties are set."
Check here.
Generally speaking, applications should not have to configure a
ConnectionProvider explicitly if using one of the Hibernate-provided
implementations. Hibernate will internally determine which
ConnectionProvider to use based on the following algorithm:
If hibernate.connection.provider_class is set, it takes precedence
else if hibernate.connection.datasource is set → Using DataSources
else if any setting prefixed by hibernate.c3p0. is set → Using c3p0
else if any setting prefixed by hibernate.proxool. is set → Using
Proxool
else if any setting prefixed by hibernate.hikari. is set → Using
Hikari
else if hibernate.connection.url is set → Using Hibernate’s built-in
(and unsupported) pooling
else → User-provided Connections
Other than this, the fastest way to check is to go in database server and see whether the idle connection have been created or not.
I have been having trouble with Hibernate and Mysql timeout error.I am also using properties of c3p0(connection provider). After my Hibernate/MySQL have been running after 8 hours(which is default timeout value in Mysql), I have the exception. But it doesn't help.
property for auto reconnect also not working.
Here is my Hibernate Configuration:
<property name="connection_provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
**<property name="connection.autoReconnect"> true</property>
<property name="connection.autoReconnectForPools">true</property>**
<property name="connection.is-connection-validation-required">true</property>
<property name="c3p0.validate">true</property>
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_second_level_cache">false</property>
<property name="c3p0.idle_test_period">20</property>
<property name="c3p0.timeout">40</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.acquireRetryAttempts">10</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.maxIdleTime">300</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.minPoolSize">10</property>
<property name="c3p0.preferredTestQuery">select 1;</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/doqeap</property>
<property name="connection.user">root</property>
<property name="connection.password">*******</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.jdbc.batch_size">0</property>
<mapping></mapping>
please help me to sort out this problem.
Thanks
If the Connection timeout is the issue, then Connection testing should eliminate, wither via tests on checkout (reliable but imposes a client visible performance cost) or tests on checking + idle tests.
Looking at you config params, it looks like you mean to set tests on checkouts and idle tests. I'd expect that c3p0 would eliminate timed out Exceptions before your app saw them. If that hasn't happened, it'd be interesting to see two things: 1) c3p0's config, which gets logged at INFO when the pool is initialized -- is c3p0, through the hibernate layer, seeing the configuration you intend? 2) the Exception that your app receives when it encounters the stale Connections.
Good luck!