0

In my project I am using two oracle datasources for one of the datasource Im using LocalSessionFactoryBean and for another LocalContainerEntityManagerFactoryBean I am mapping the datasources accordingly but when Im trying to get sessionFactory for LocalSessionFactoryBean the datasource details are that of the other. Dono what I am doing wrong.

<tx:annotation-driven transaction-manager="xxxTransactionManager" /> <bean id="xxxEntityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceXmlLocation" value="classpath:persistence.xml" /> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> <bean class="org.modelmapper.ModelMapper" id="modelMapper"/> <bean id="xxxEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> <property name="entityManagerFactory" ref="xxxEntityManagerFactoryBean" /> </bean> <bean id="xxxTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="xxxEntityManagerFactoryBean" /> <property name="nestedTransactionAllowed" value="true" /> <property name="dataSource" ref="dataSource" /> <property name="jpaDialect" ref="jpaDialect" /> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 

in another xml file that is imported i have

<bean id="sessionFactoryNew" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource1" /> <property name="packagesToScan" value="com.citi.aml.npa.domain.api" /> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> </props> </property> </bean> <bean id="xxxTransactionManagerNew" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactoryNew" /> <qualifier value="xxxtransaction"/> </bean> 

in app context.xml i have the datasources like

<beans profile="local"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.oracle.driverClassName}" /> <property name="username" value="${db.oracle.username}" /> <property name="password" value="${db.oracle.password}" /> <property name="url" value="${db.oracle.url}" /> </bean> <context:property-placeholder location="classpath*:/config/local/yyy.properties" ignore-unresolvable="true"/> </beans> <beans profile="local"> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.oracle.driverClassName}" /> <property name="username" value="${db.oracle.username}" /> <property name="password" value="${db.oracle.password}" /> <property name="url" value="${db.oracle.url}" /> </bean> <context:property-placeholder location="classpath*:/config/local/xxx_db.properties" ignore-unresolvable="true"/> </beans> 

in Dao impl calss i have

@Repository public class OracledbTestRepository implements TestRepository { @Autowired @Qualifier(value = "sessionFactoryNew") SessionFactory sessionFactory; @SuppressWarnings("unchecked") @Override public List<xxx> getTestData() { Session session = sessionFactory.getCurrentSession(); System.out.println(sessionFactory); List<xxx> xxxTestDatas = session.createCriteria(xxx.class).list(); return xxxTestDatas; } } 

any help will be appreciated.

1 Answer 1

2

Your bean configuration looks a bit wonky to me. You have two different profiles with the same name. If these two datasources need to run at the same time, it would be simpler to consolidate your database properties into a single file, and place both datasource configurations within the same profile element.

So, for the consolidated database.properties, do something like:

db1.oracle.url=jdbc:oracle:thin:@server1... db1.oracle.username=foo ... db2.oracle.url=jdbc:oracle:thin:@server2... db2.oracle.username=bar 

And for the bean config, use something like:

<beans profile="local"> <context:property-placeholder location="classpath*:/config/local/database.properties" ignore-unresolvable="true"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db1.oracle.driverClassName}" /> <property name="username" value="${db1.oracle.username}" /> <property name="password" value="${db1.oracle.password}" /> <property name="url" value="${db1.oracle.url}" /> </bean> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db2.oracle.driverClassName}" /> <property name="username" value="${db2.oracle.username}" /> <property name="password" value="${db2.oracle.password}" /> <property name="url" value="${db2.oracle.url}" /> </bean> </beans> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.