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.
Related
I have legacy hbm.xml configured persistent classes and Annotated persistent classes, so currently we have to specify per which session factory is being used in the DAO bean.
Unfortunately, that means that I'm running into an issue where we have a mix of DAO's that I'd like to use, aren't all associated to a single session factory.
I'd like to combine both into a single session factory bean since we can't just move everything over all at once.
How would I go about doing this?
Note: my current workaround is to Annotate some of the xml configured ones and then create two DAO beans: one for each Session Factory.
HBM.XML:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Annotated:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Thanks M.Deinum and orid:
I was overthinking this.
There was never a need for two session factories.
So I just had to refactor the context file to:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
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 ?
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?
Here is my hibernate config
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:/localhost/testDB"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan" value="sample.model"/>
<property name="annotatedPackages">
<list>
<value>sample.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
I included this dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
Here is the exception
Caused by: java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.hibernate.cfg.Configuration.addAnnotatedClass(java.lang.Class)
at org.springframework.util.ClassUtils.getMethod(ClassUtils.java:627)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<clinit>(LocalSessionFactoryBuilder.java:79)
... 39 more
What am I missing here?
It looks like your hibernate annotations jar file is the incorrect version.
You should already have hibernate-core as a dependency. If so you can comment out your hibernate-annotations dependency in your POM file.
The solution was to use hibernate 3 and Adding this
<prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
So the final config is
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan" value="sample.model"/>
<property name="annotatedPackages">
<list>
<value>sample.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
</props>
</property>
</bean>
Ok, I have this spring hibernate xml configuration.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
<value>com/abc/def/c.xml</value>
<!--
And so on, about 50 xml for example
How can I separate list value above into 5 file for example?
ex I have h1.xml (or h1.txt) that contain
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
I have h2.xml (or h2.txt) that contain
<value>com/abc/def/c.xml</value>
<value>com/abc/def/d.xml</value>
so the mappingResources just read from the files (more than 1) than contain all mapping objects
-->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
I have commented the question in the xml configuration above.
Thanks
You can just use
<property name="mappingLocations">
<list>
<value>classpath:com/abc/def/*.xml</value>
</list>
</property>