In our project's code, we are initialising slave DB Spring HibernateTransactionManager through xml as shown below:
<bean id="slaveSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">com.swiggy.delivery.deliveryboy.model.dao.CustomMySql5Dialect</prop>
<prop key="hibernate.show_sql">${hibernate.slave.showSql}</prop>
<prop key="hibernate.format_sql">${hibernate.slave.formatSql}</prop>
<prop key="hibernate.connection.url">${hibernate.slave.connection.url}</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.ReplicationDriver</prop>
<prop key="hibernate.connection.username">${hibernate.slave.connection.username}</prop>
<prop key="hibernate.connection.password">${hibernate.slave.connection.password}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.slave.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.slave.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">0</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.c3p0.max_idle_time_excess_connections">60</prop>
</props>
</property>
</bean>
<bean id="slaveTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="slaveSessionFactory" />
<property name="defaultTimeout" value="10" />
</bean>
Here, I want to understand that for DB read timeouts, which value would be used during the transaction, the hibernate.c3p0.timeoutvalue of 300 ms or the defaultTimeout value of 10 ms.
I tried to go through both Spring and Hibernate docs for this but could not get a clear answer on whether the defaultTImeout would override the hibernate.c3p0.timeout or not.
My hunch is that the timeout value picked would be 300 ms. But cannot confirm that.
Related
I need a solution about tomcat datasource configuration.
I have 2 nginx front of my tomcats.These are feeding my app, these are Webservice.
Tomcat machines and database are in different ip blog so its going firewall in each request.
When i started my tomcats everythigs well.But for example after 10 hour our after 20 hour later everythings going bad.That tomcats can not set connection.All service down.I share my datasource file and hope you find some solution or some other suggestions other than datasource file.
http://www.springframework.org/schema/beans ----
http://www.springframework.org/schema/context/spring-context.xsd">
<property name="connectionProperties">
<props merge="default">
<prop key="v$session.program">ws_${server}</prop>
</props>
</property>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">1</prop>
<prop key="MaxLimit">120</prop>
<prop key="InitialLimit">5</prop>
<prop key="MaxStatementsLimit">50</prop>
<prop key="ConnectionWaitTimeout">30</prop>
<prop key="InactivityTimeout">600</prop>
<prop key="AbandonedConnectionTimeout">180</prop>
<prop key="PropertyCheckInterval">300</prop>
<prop key="ValidateConnection">true</prop>
<prop key="TimeToLiveTimeout">600</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="nativeJdbcExtractor">
<bean class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="com.ws.oracle.pool.JndiExporter">
<property name="bean" ref="dataSource" />
<property name="jndiName" value="dsName" />
</bean>
I have a problem integrating Spring, Hibernate, and Apache DBCP. I have used the DBCPConnectionProvider from here.
I have the following xml configuration for the above said.
<context:component-scan base-package="com.my.app"/>
<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.my.app.model</value>
</list>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/app</prop>
<prop key="hibernate.connection.username">foo</prop>
<prop key="hibernate.connection.password">bar</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.DBCPConnectionProvider</prop>
<!-- Connection pooling related properties -->
<prop key="hibernate.dbcp.initialSize">8</prop>
<prop key="hibernate.dbcp.maxActive">20</prop>
<prop key="hibernate.dbcp.maxIdle">5</prop>
<prop key="hibernate.dbcp.minIdle">0</prop>
<prop key="hibernate.dbcp.maxWait">10000</prop>
<prop key="hibernate.dbcp.minEvictableIdleTimeMillis">180000</prop>
<prop key="hibernate.dbcp.timeBetweenEvictionRunsMillis">180000</prop>
<prop key="hibernate.dbcp.testWhileIdle">true</prop>
<prop key="hibernate.dbcp.testOnReturn">true</prop>
<prop key="hibernate.dbcp.validationQuery">select 1</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
When the database schema i.e. app is empty two tables will be created in mysql. But I am getting NullPointerException in the getConnection() method of the DBCPConnectionProvider that I mentioned. That means the dataSource is not instantiated. I think I have configured everything in the xml. What am I missing. How do you configure DBCP version 1.4 with Spring and Hibernate? Please provide your insights.
Here is the stack trace:
Caused by: java.lang.NullPointerException
at org.hibernate.connection.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:209)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
... 82 more
Don't do it like that. Configure the datasource you want to use in Spring as well as Hibernate. Ditch the hibernate.dbcp and hibernate.connection properties.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/app"/>
<property name="username" value="foo"/>
<property name="password" value="bar"/>
// Other DBCP properties here
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"
<property name="packagesToScan">
<list>
<value>com.my.app.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Just add the dataSource property to your AnnotationSessionFactoryBean as dependency and done. Note you don't need the configurationClass property as it is already annotation based.
A tip I wouldn't suggest using Commons-DBCP anymore as a datasource instead take a look at HikariCP as a better datasource implementation.
For more information in integrating/configuring Hibernate with Spring I suggest this section of the Reference Guide.
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.genpact.cards.services.database"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="connection.pool_size">1</prop>
<prop key="maxActive">1</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
</props>
</property>
</bean>
So here it goes..
I have configured two jndi in my glassfish server for communicating two different databases.And in my applicationContext.xml I have following configuration
<!-- Database connection 1 STARTS HERE-->
<jee:jndi-lookup id="JNDI1" jndi-name="jdbc/db1"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="JNDI1" />
<!-- <property name="annotatedClasses">
<list>
<value>com.test.db1.Person</value> //THIS WORKS FINE
</list>
</property>-->
<property name="packagesToScan" value="com.test.db1" /> //THIS IS STRANGE
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Database connection 1 ENDS HERE-->
<!-- Database connection 2 STARTS HERE-->
<jee:jndi-lookup id="JNDI2" jndi-name="jdbc/db2" />
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="JNDI2" />
<property name="packagesToScan" value="com.test.db2" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Database connection 2 ENDS HERE-->
In package com.test.db1 I have only Person class and in com.test.db2 package I have two entities named Employee and Company.
The problem occurs when I use packageToScan property in both SessionFactory beans all 3 tables are created in both databases.And when I explicitly define entity classes using annotatedClasses property tables are generated in relevant databases as configured in jndi.I am not getting why this kind of results I am getting while using packageToScan.
I'll be grateful If someone can explain me in detail.
Thank you
may i know as my configuration is done directly on applicationContext.xml, i do not have persistence.xml . by default this is resource_loca or jta? do i need to add extra parameter if i want to use jta?
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<!-- xdb is defined by running the hsqldb as xdb (see above) -->
<property name="url">
<value>jdbc:oracle:thin:#theserver:1521:appsdb</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>test</value>
</property>
</bean>
<bean id="annotatedsessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="com.company.x.model" >
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.cache.provider_class">
com.company.x.services.ExternalEhCacheProvider
</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
RESOURCE_LOCAL only applies to JPA EntityManager, not to a Hibernate SessionFactory. Hibernate's Spring integration is rather smoother than it is with JPA, and so the only thing that determines the transactional behaviour is which transaction manager you use with it (either HibernateTransactionManager or JtaTransactionManager). It'll work with either one without you having to explicitly configure the SessionFactory.