Announcement Announcement Module
Collapse
No announcement yet.
Problem multiple datasource Spring/hibernate/Jpa Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem multiple datasource Spring/hibernate/Jpa

    hi,

    i got one probleme

    i have configured 2 db in my app, but my services always connect to the first, i don't know why...
    i can't get data from the second

    my code :
    persistence-context
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd 
    	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "
    	xmlns:p="http://www.springframework.org/schema/p">
    
    	<bean id="jpaDataSource1"
    		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/ep" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    	<bean id="jpaDataSource2"
    		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/ep2" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    	
    	<bean id="persistenceUnitManager"
    		class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    		<property name="persistenceXmlLocations">
    			<list value-type="java.lang.String">
    				<value>classpath*:META-INF/persistence.ep1.xml</value>
    				<value>classpath*:META-INF/persistence.ep2.xml</value>
    			</list>
    		</property>
    		<property name="dataSources">
    			<map>
    				<entry key="jpaDataSource1" value-ref="jpaDataSource1" />
    				<entry key="jpaDataSource2" value-ref="jpaDataSource2" />
    			</map>
    		</property>
    		<property name="defaultDataSource" ref="jpaDataSource1" />
    	</bean>
    
        <bean
            class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
            <property name="defaultPersistenceUnitName" value="pu1" />
        </bean>
    
    	<bean id="EntityManagerFactoryPU2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="pu2" />
     		<property name="persistenceXmlLocation"
    			value="classpath:META-INF/persistence.ep2.xml" />
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    		<property name="dataSource" ref="jpaDataSource2" />
    		<property name="jpaVendorAdapter">
    			<bean
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:generateDdl="false">
    			</bean>
    		</property>
    	</bean>
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    		<property name="dataSource" ref="jpaDataSource1" />
     		<property name="persistenceUnitName" value="pu1" />
    		<property name="persistenceXmlLocation"
    			value="classpath:META-INF/persistence.ep1.xml" />
    		<property name="jpaVendorAdapter">
    			<bean
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:generateDdl="false"/>
    		</property>
    	</bean>
    
    
    	<bean id="jpaTransactionManager"
    		class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory"
    			ref="entityManagerFactory" />
    	</bean>
    	<tx:annotation-driven transaction-manager="jpaTransactionManager" />
    </beans>
    persistence.ep (both are same)
    Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
    	http://java.sun.com/xml/ns/persistence 
    	http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    	version="1.0">
    	<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
    		<properties>
    			<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
    			<property name="hibernate.show_sql" value="true" />
    			<property name="hibernate.format_sql" value="false" />
    			<property name="hibernate.jdbc.batch_size" value="100" />
    		</properties>
    	</persistence-unit>
    </persistence>
    by default all services connect to pu1 but for user, i have to connect to pu2
    in my userDaoImpl

    my user-context.xml

    Code:
    	<bean id="userService" class="impl.UserServiceImpl">
    		<property name="userDao" ref="userDao"/>
    	</bean>
    	
    	<bean id="userDao" class="impl.UserDaoImpl">
    		<property name="entityManagerFactory" ref="EntityManagerFactoryPU2"/>
    	</bean>
    Code:
    public class UserDaoImpl extends HibernateJpaDaoImpl implements UserDao{
    	
    	@PersistenceContext(unitName="pu2")
        private EntityManager entityManager;
    	
    	public UserDaoImpl() {
    	}
    	@Override
    	public List<User> list() {
    		Query query= entityManager.createQuery("from User");
    		List list=query.getResultList();
    		return list;
    	}
    this method returns nothing because my table user1 in pu1 is empty but i want the data from the table user from pu2...

    It will be one day i'm on it, can't find any solution to get work properly 2 database...

    thx for help
Working...
X