Im using hibernate3 and springframework.
I want to set c3P0 Pool for hibernate.connection.provider_class but apparently LocalDataSourceConnectionProvider was set.
In Hibernate.log I see this:
[Level: INFO]Initializing connection provider:
org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
I think org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider is hibernate default connection provider class for pooling and as I read it's wrong to use in production. Is that correct?
I want to set org.hibernate.connection.C3P0ConnectionProvider and manage pool connection with c3p0
This is my hibernate config:
<bean id="c3p0Datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="jdbcUrl">
<value>.......</value>
</property>
<property name="user">
<value>.......</value>
</property>
<property name="password">
<value>.......</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="c3p0Datasource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.default_schema">.....</prop>
<prop key="hibernate.hbm2ddl.auto">UPDATE</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.isolation">2</prop>
</props>
</property>
</bean>
Any suggestions?
Related
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.
I want my program to exit if it cannot connect to the database on startup. Currently this connection is setup using the following:
application-context.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</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>
<property name="packagesToScan" value="com.template" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.ejb.naming_strategy">${naming_strategy}</prop>
<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.connection.autoReconnectForPools">true</prop>
<prop key="hibernate.connection.check-valid-connection-sql">SELECT 1</prop>
<prop key="hibernate.connection.failOverReadOnly">false</prop>
<prop key="hibernate.connection.maxReconnects">${maxreconnects}</prop>
<prop key="hibernate.connection.initialTimeout">${reconnect.interval}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Repository:
#Repository
public interface Repository extends CrudRepository<Object, String> {
}
It's not obvious where I should place code to catch the runtime exceptions created by the connection failure. Are there any other settings I can use to exit if the database doesn't exist.
Usually, spring will stop, if it could not create a bean at startup. If DB connection fails, then it will stop automatically anyway. Do you want to catch that exception, and do something before exit ?
I have a DispactherServlet.xml file which has the hibernate file configuration as
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>com/dibya/hbm/resource/model.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.username">root</prop>
<prop key="hibernate.password"></prop>
<prop key="hibernate.url">jdbc:mysql://localhost/test</prop>
<prop key="hibernate.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id = "hibernateTemplate" class = "org.springframework.orm.hibernate3.HibernateTemplate">
<property name = "persister">
<ref bean = "sessionFactory"/>
</property>
</bean>
In my controller, I have
#RequestMapping(value = "Hello.htm")
public String HelloWorld(Model model) {
boolean is = persister.isAllowCreate();
Person person = new Person();
person.setName("dibya");
persister.saveOrUpdate(person);
System.out.println("This is called"+is);
return "HelloWorld";
}
I am getting this error message:
HTTP Status 500 - Request processing failed; nested exception is java.lang.UnsupportedOperationException: The user must supply a JDBC connection
Please tell me what am I forgetting to add.
Your hibernate config file is using wrong parameter names. Correct param names are as below:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.dialect
hibernate.show_sql
Correct your param names and try again.
EDIT: Refer this link for detailed list of param names: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
You can configure like this :
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:file:/local/hsqldb</prop>
<prop key="hibernate.connection.username">sa</prop>
<prop key="hibernate.connection.password"></prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">${gloss.database.hibernate.show_sql}</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/core/domain/User.hbm.xml</value>
</list>
</property>
</bean>
I am trying to config two sessionFactories using spring. My config looks similar to the one listed here
Here's my config.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
...Mappings
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</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.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
</props>
</property>
</bean>
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${hibernate.connection.mirror_url}</value>
</property>
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="username">
<value>${hibernate.connection.mirror_username}</value>
</property>
<property name="password">
<value>${hibernate.connection.mirror_password}</value>
</property>
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource2" />
</property>
<property name="mappingResources">
<list>
...Mappings
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</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.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
</props>
</property>
</bean>
Then each dao gets a different sessionFactory assigned
<bean id="productDao"
class="test.dao.ProductDaoHibernate">
<property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>
<bean id="currencyDao"
class="test.dao.CurrencyDaoHibernate">
<property name="sessionFactory"><ref bean="sessionFactory2" /></property>
</bean>
This config gets loaded when its added to the context
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/test-data.xml /WEB-INF/classes/test-services.xml ... </param-value>
</context-param>
The problem shows whenever I start the server each sessionFactory built, but at the end of the second one this shows up:
[INFO] [org.springframework.beans.factory.support.DefaultListableBeanFactory]:? - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#97aaa6: defining beans [... Many elements...]; root of factory hierarchy
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing
Any help, or lead would be appreciated, if you need more info please ask
spring4.2 & hibernate5.1.5
#Bean
public LocalSessionFactoryBean aaaSessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(smsDataSource());
localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
return localSessionFactoryBean;
}
#Bean
public LocalSessionFactoryBean bbbSessionFactoryMultiple() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(smsDataSource());
localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
return localSessionFactoryBean;
}
public class xxx extends HibernateDaoSupport{***
#Autowired
public void anyMethodName(#Qualifier("aaaSessionFactory") SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
Actually this is supposed to be a comment, but don't have enough reputation (requires 50 points)
It seems you are trying to create 2 different bean id's which are of exactly same configuration. One way is to figure out whether 2 different session objects pointing to same configuration is required. Other way is to try adding the configurations in different files and load separately. Please add how you r trying to use these configurations in code.
I have a web application created with Spring data, which uses JpaRepository (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) after some server uptime I have noticed that heap is growing all the time.
After taking a heap dump it shows that heap contains much more entities than I am currently using.
So my question is how to manage EntityManager to clear entities that are not being used any more?
Here is jpa configuration:
<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" p:database="${jdbc.databaseType}"
p:generateDdl="true" p:showSql="false"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.ejb.naming_strategy">com.example.db.repository.FixedDefaultComponentSafeNamingStrategy</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager" p:entityManagerFactory-ref="entityManagerFactory">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>