I am getting JDBC transaction failed at first hit to DB. I found one problem is that. My mysql is idle for 10 to 12 hours. so may be after 10 hours when i am going to hit at login time getting exception
my configuration is
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.isolation">2</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
</props>
</property>
<property name="packagesToScan" value="com.perennialsys.salesoptimization.vo" />
</bean>
please help for this. thanks in advanced
one more thing that i got from googling is idle_test_period should not exceeds timeout. is this the reason?
The database server may close a connection on its side after a certain amount of time - causing some error in your application, because it'll attempt to send a query on a connection which is no longer available on the server side.
In order to avoid this you can let the pool periodically check a connection for it's validity. This is what idle_test_period is for.
timeout is the timespan after which the pool will remove a connection from the pool, because the connection wasn't checked out (used) for a while and the pool contains more connections than c3pO.min_size.
Please keep in mind, that hibernate.c3p0.idle_test_period value must never exceed that of hibernate.c3p0.timeout. Otherwise C3P0 will never detect connections that has been closed.
Thanks,
Rajesh
Related
I have an Spring application with quartz scheduler for batch processing. It was working fine from last 4 years. There is a batch which runs on every 1 minute. But from last few days suddenly when the batch is trying to fetch some data we are getting org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
This batch runs throughout the day but some times gives this exception and all other batches fails to connect to MySQL. So we are restarting the application and it is working fine.
Data source config
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${DATABASE_URL}</value>
</property>
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>${DATABASE_USER}</value>
</property>
<property name="password">
<value>${DATABASE_PASSWORD}</value>
</property>
</bean>
Session factory connection pooling configuration
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<!-- Configurations specific to c3p0 connection pooling -->
<prop key="hibernate.c3p0.acquire_increment">5</prop>
<prop key="hibernate.c3p0.idle_test_period">1800</prop>
<prop key="hibernate.c3p0.max_size">600</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
</props>
</property>
<property name="annotatedClasses">
<list> .... </list>
</property>
</bean>
May be it is the problem with the mysql running your db-server.
change the mysql configuration in your db-server
add the bind-address in mysql.cnf under [mysqld] section
bind-address=your_spring_application_ip
also change the accessing host for the mysql user.
run the following query in mysql
update mysql.user set host='%' where user='your_username';
flush privileges;
then restart the mysql service
sudo systemctl restart mysql
Recently we had kind of the same issue. Our application failed to connect to aws Aurora instance. What we ended up doing changing sql driver to mariadb one. Hope this will help.
We recently migrated from Hibernate 3.2.3, Spring 3.2.2 to Hibernate 4.3.11 and Spring 4.2.1 on Weblogic 10.3
Now in a transaction when hibernate objects are created and added to a hibernate object's persistent collection and later queried with HQL(using hibernateTemplate) or Hibernate's "Query" feature (in the same transaction and before the end of the transaction), Hibernate fails to find the objects that were added. This worked with Hibernate 3 and Spring 3 before the upgrade, but now fails.
e.g. In Psuedo code, say i have a library class with the following property.
class Library{
private Collection<Books> books;
}
In a transaction, i do the following -
...
Book book1 = new Book();
book1.setAuthor("Patrick Holt");
library.getBooks().add(book1);
and later on in the same transaction, Hibernate's "Query" is used with a Filter to look for books by the author, like so
Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
Query q = s.createFilter(library.getBooks(), "where this.author = :authorName");
q.setParameter("authorName", "Patrick Holt");
List l = q.list();
q.list() in the above example returns 0 results. This would return 1 result before the upgrade. After the upgrade i get 0 results.
I read about some changes in the currentSessionContext in upgrades to Hibernate 4 and Spring 4, but I'm not sure what needs to change in order for the behavior to be just like before the upgrade, without the code having to change.
Out hibernate objects are defined in hbm.xml files and here's my configuration of the session factory.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<!--
Tried adding the following properties, but it didn't work either
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="mappingJarLocations">
<list>
<value>/WEB-INF/lib/app-1.0.jar</value>
</list>
</property>
</bean>
Transaction Manager definition:
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>
The datasource is made available over jndi.
Any ideas on what i need to do to fix this issue?
The issue i was having is exactly as described in Spring Jira Issue SPR-13848. As i understand, it seems like the OpenSessionInViewFilter opens a request scoped session early and there are issues with syncing with a transaction that's started later.
I could not get rid of my OSIV because of the impact that it would cause throughout my application.
However, i found out that hibernate has a property that will accomplish something similar to prevent lazy loading -
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
This solved the issue. Adding this property allowed me to remove the OSIV, and hibernate's AUTO flush mode works as expected in a transaction now. I also used the CMTTransactionFactory. All my hibernate properties:
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
</props>
I have following bean:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql">
<value>${hibernate.show_sql}</value>
</property>
<property name="generateDdl">
<value>${generateDdl}</value>
</property>
<property name="databasePlatform">
<value>${databasePlatform}</value>
</property>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
<prop key="hibernate.archive.autodetection">${hibernate.archive.autodetection}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.comment_sql">${hibernate.comment_sql}</prop>
<!-- optimization settings -->
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<!-- c3p0 connection pool settings -->
<prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire_increment}</prop>
<prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.maxIdleTime">${hibernate.c3p0.maxIdleTime}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.c3p0.min_pool_size">${hibernate.c3p0.min_pool_size}</prop>
<prop key="hibernate.c3p0.max_pool_size">${hibernate.c3p0.max_pool_size}</prop>
<prop key="hibernate.c3p0.preferredTestQuery">${hibernate.c3p0.preferredTestQuery}</prop>
<prop key="hibernate.c3p0.idleConnectionTestPeriod">${hibernate.c3p0.idleConnectionTestPeriod}</prop>
<prop key="hibernate.c3p0.autocommit">${hibernate.c3p0.autocommit}</prop>
<!-- second level cache settings -->
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
</props>
</property>
</bean>
Can anyone explain to me whats the difference between hibernate.connection.autocommit and hibernate.c3p0.autocommit? Also there are another equal properties . Whats the difference? Also what happens if they are different which one will prevail?
According to the c3p0 documentation :
autoCommitOnClose Must be set in c3p0.properties, C3P0 default: false
The JDBC spec is unfortunately silent on what should happen to unresolved, pending transactions on Connection close. C3P0's default policy is to rollback any uncommitted, pending work. (I think this is absolutely, undeniably the right policy, but there is no consensus among JDBC driver vendors.) Setting autoCommitOnClose to true causes uncommitted pending work to be committed, rather than rolled back on Connection close.
As hibernate.c3o0.autocommit drives this settings, we can say that the difference is that hibernate.connection.autocommit is just like wrapping every query with begin transaction; and then commit.
Both are not recommended.
After upgrading Hibernate version from 4.3.9.Final to 5.0.2.Final, Tomcat Server start-up time has increased.
After debugging, I realized that hibernate takes too much time in adding mapping locations (*.hbm.xml files) in its metadata sources.
I have added mapping location in session factory using following code, and in my project there are around 1000 hbm.xml files.
<bean id="baseSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.jdbc.fetch_size">300</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath*:com/*/**/*.hbm.xml</value>
</list>
</property>
</bean>
While starting tomcat server, I debugged and fournd that in method
org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet
following for loop takes too much time to add all mapping locations in metadata sources object of configuration class.
if (this.mappingLocations != null) {
// Register given Hibernate mapping definitions, contained in resource files.
for (Resource resource : this.mappingLocations) {
sfb.addInputStream(resource.getInputStream());
}
}
Is there any solution to improve performance here? Anyone noticed such issue after upgrading to Hibernate-5?
I'm getting this error:
2014-11-14 17:39:44 [WARN]SqlExceptionHelper:143 SQL Error: 0, SQLState: 08S01
2014-11-14 17:39:44 [ERROR]SqlExceptionHelper:144 Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2014-11-14 17:39:44 [ERROR]BackupService:245 org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
Seems that Hibernate has a small transaction timeout for my job, but I can not find what is the default timeout value. I did not find it also in hibernate documentation, I find how to change it, but not the default value.
This is my hibernate configuration:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.test.db.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.search.autoregister_listeners">false</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
</props>
</property>
</bean>
Do you know what is the default timeout value for an hibernate transaction with this configuration?
I got the same exception. I googled and found your question without answer :)
I changed "Timeout Seconds:" from 300 to 1000 (possible to increase more, it depends on your transaction timeout) on "mydomain->Configuration->JTA" on Weblogic Console or increase JTA timeout on your application server.
Then, for hibernate.cfg.xml you may check this answer: Hibernate/MySQL Connection Timeout
It works.