After implementing C3P0 the page keeps refreshing - java

I am trying to implement C3P0 into my hibernate. I have as follows:
hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<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://IPaddress</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size">20</property>-->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">1000</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">864000</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">30</property>
<property name="hibernate.c3p0.initialPoolSize">10</property>
<property name="hibernate.c3p0.maxPoolSize">100</property>
<property name="hibernate.c3p0.minPoolSize">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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">true</property>
<!-- Mappings -->
<mapping class="com.nebuilder.ats.pojo.TopicsDetails"/>
<mapping class="com.nebuilder.ats.pojo.GroupsDetails"/>
<mapping class="com.nebuilder.ats.pojo.ModulesDetails"/>
<mapping class="com.nebuilder.ats.pojo.TraineesDetails"/>
<mapping class="com.nebuilder.ats.pojo.ColoursDetails"/>
<mapping class="com.nebuilder.ats.pojo.CustomersDetails"/>
<mapping class="com.nebuilder.ats.dao.MusicStoreDaoImpl"/>
</session-factory>
</hibernate-configuration>
ApplicationContext.xml
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
Both of the files are in my resources folder. They seem to be working, but the problem is that my page keeps loading or refreshing without displaying any information when I try to access the database.
I am using jars as follow - hibernate-c3p0 3.6.3.Final, hibernate-core 3.6.3.Final, c3p0 0.9.1.2

Did you try to debug your application to see where is the problem ?
Moreover, it seems to be a little bit excessive to define a timeout of 864 000 ms, isn't it ?

Related

Hibernate to MySQL automatic loose connection even use autoReconnect=true in JDBC URL

I am trying following JDBC URL and properties but it's not working with autoReconnect=true.
<properties>
<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/gift20da_jobportal?autoReconnect=true</property>
<property name="hibernate.connection.username">gift20da_jobportal</property>
<property name="hibernate.connection.password">Jobportal#job</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</properties>
Is there any another way to keep connection?
this got solved my problem.
<!-- Sessions and transactions -->
<!-- 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>
<property name="current_session_context_class">thread</property>
add this code in hibernate.cfg.xml , for reference http://liferay-portlets.blogspot.in/2009/01/meanwhile-hibernate-mysql-problems.html .

Spring + Hibernate + Sql Server connection failing

I am getting the following error when I am using a combination of Spring, Hibernate and SQL Server.
19:17:09,137 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (MSC service thread 1-8) HHH000319: Could not get database metadata: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host falmumapp20/testdb, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
It could not be just a TCP IP problem, because if I work without Spring, I am able to connect to SQL Server using hibernate.
Below is my applicationContext.xml
<!-- Resource Definition -->
<!-- Data Source Connection Pool -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://falmumapp20:1433" />
<property name="username" value="tima"/>
<property name="password" value="chalk#"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/trun/hbm</value>
</list>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--Definition of Transaction Manager-->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="IDataService" class="com.trun.service.DataServiceImpl">
</bean>
<bean id="EventServlet" class="com.trun.servelet.LoginServelet">
<property name="IDataDAO" ref="IDataDAO" />
</bean>
And here is my Hibernate Configuration file -
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/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://falmumapp20:1433;databaseName=testdb; user=tima;password=chalk#;</property>
<property name="hibernate.connection.username">tima</property>
<property name="hibernate.connection.password">chalk#</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Mapping files -->
</session-factory>
You are not passing database name to the connection url
<property name="url" value="jdbc:sqlserver://falmumapp20:1433;databaseName=testdb" />
or
<property name="url" value="jdbc:sqlserver://falmumapp20:1433/testdb" />

Migrate Hibernate managed C3P0 Pool to Spring Managed

I'm Spring(yfying) my application that uses Hibernate + C3P0 for the connection pool. I'm using a managed hibernate context for specific reasons. I'm using a utility class "HibernateUtil" for Session handling. For the first migration to Spring I am creating an ApplicationContext and retrieving a SessionFactory bean in HibernateUtil replacing the code that used to build the SessionFactory. When I create a session factory bean completely from my old hibernate.cfg.xml in Spring everything works as expected:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>
</bean>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.username">user</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=convertToNull</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.show_sql">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!-- configuration pool via c3p0-->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="connection.isolation">2</property>
<property name="hibernate.c3p0.acquire_increment">3</property>
<property name="hibernate.c3p0.idle_test_period">120</property> <!-- seconds -->
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.timeout">1800</property>
<!-- mapping files -->
.......
If I externalize the connection pool (I also remove all the connection settings from hibernate.cfg.xml), my transactions do not work properly.
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>
</bean>
I've tried specifying hibernate.transaction.factory_class and moving hibernate properties to Spring bean configuration instead of using hibernate.cfg.xml all to no avail. I cannot completely switch to Spring Transaction management just yet.
Imho you should try to move everything to Spring. In my experience when it is to mixed up there are a lot of problems. Is there a particular reason why you cannot yet set up a spring managed transaction management using:
<!-- Transaction Management -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
From your description it is quite dificult to find out where exactly the problem is occuring is it with the transactions or with the sessionFactory?
Another point you might be causing your sessionFactory problems when using the dataSource outside of the hibernateProperties is that the ConnectionProvider implementation Hibernate is using is changing. If you specify a dataSource Hibernate will use DataSourceConnectionProvider(http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/connection/DatasourceConnectionProvider.html) whereas with setting the data source in the hibernate config Hibernate will use the DriverManagerConnectionProvider (http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/connection/DriverManagerConnectionProvider.html)
The differences between these two might be the cause of your problems.

how to solve Null user or password not supported in THIN driver

i am getting Null user or password not supported in THIN driver when configure jpa with hibernate configuration in seam
components.xml
<persistence:hibernate-session-factory name="hibernateSessionFactory" cfg-resource-name="hibernate.cfg.xml"/>
<persistence:managed-hibernate-session name="session"
auto-create="true"
session-factory-jndi-name="java:/mobeeSessionFactory"/>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
projectname-ds.xml
<local-tx-datasource>
<jndi-name>mobeeadminDataSource</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:#localhost:1521:mobee</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>mobeemigrate</user-name>
<password>mobeemigrate</password>
</local-tx-datasource>
The password is missing from hibernate.cfg.xml.
To fix include the following in your hibernate.cfg.xml file as shown below:
<property name="hibernate.connection.password">mobeemigrate</property>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.connection.password">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
Your hibernate.cfg.xml has the following property:
<property name="hibernate.connection.username">mobeemigrate</property>
But not the corresponding password one:
<property name="hibernate.connection.password">mobeemigrate</property>

Need hibernate properties for mysql

I can't seem to find an example of hibernate properties for mysql.
Is there a link that has an example?
I have one for hsql:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.jdbc.batch_size=0
</value>
</property>
</bean>
<?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.url">jdbc:mysql://localhost/firsthibernate</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">r</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- mapping files -->
<mapping resource="de/laliluna/example/Honey.hbm.xml" />
</session-factory>
</hibernate-configuration>
Based on your current Spring config file I think it would be:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc:mysql://localhost/mydb
hibernate.connection.username=dbuser
hibernate.connection.password=dbpass
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.jdbc.batch_size=0
</value>
</property>
</bean>

Categories

Resources