使用spring在一个应用程序中维护两个数据库

时间:2017-12-24 11:59:46

标签: java spring hibernate spring-mvc spring-data

我有一个用例,我必须在远程数据库中保留一些数据但是我会在本地enter code here数据库中引用数据,插入在远程数据库中正确发生但是当我正在尝试从远程数据库访问数据我无法获取数据,这里我指的是我的几个代码片段。

persistence.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
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.xsd">

<bean id="myDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />

    <!-- Added this additional line to share the connection Pool -->
    <!-- <property name="initialSize" value="1000" /> <property name="maxActive" 
        value="1000" /> <property name="maxIdle" value="10" /> -->
</bean>

<!-- This is the new Properties for the new DB of aadhaar vault -->
<bean id="myDataSource1"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${db1.url}" />
    <property name="username" value="${db1.username}" />
    <property name="password" value="${db1.password}" />

    <!-- Added this additional line to share the connection Pool -->
    <!-- <property name="initialSize" value="1000" /> <property name="maxActive" 
        value="1000" /> <property name="maxIdle" value="10" /> -->
</bean>

<!-- This is the new DB table, which is being initialized for the aadhaar 
    vault -->

<bean id="mySessionFactory1"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource1" />
    <property name="configLocation" value="classpath:hibernateNew.cfg.xml" />
    <property name="namingStrategy" ref="namingStrategy" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <!-- Specify session context -->
            <!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> -->

        </props>
    </property>
</bean>



<!-- This is the new DB table, which is being initialized for the  
    remote , ends here -->


<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    <property name="namingStrategy" ref="namingStrategy" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <!-- Specify session context -->
            <!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> -->

        </props>
    </property>
</bean>

<tx:annotation-driven />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="transactionManagerNew"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory1" />
</bean>

<bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy" />

hibernate.cfg.xml中

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="com.abc.def.model.User"></mapping>

    </session-factory>
</hibernate-configuration>

hibernateNew.cfg.xml

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="com.abc.def.model.User1"></mapping>
    </session-factory>
</hibernate-configuration>

config.properties

db1.url=jdbc:mysql://localhost:3306/new_db?useUnicode=yes&characterEncoding=UTF-8
    db1.username=root
    db1.password=password

db1.url=jdbc:mysql://localhost:3306/old_db?useUnicode=yes&characterEncoding=UTF-8
db1.username=root
db1.password=root

ABCService.Java

 @Service
public class ABCServiceImpl implements ABCService {

    @Autowired
    ABCDao abcDao;
    @Override
    @Transactional("transactionManagerNew")
    public ABC getByForeignKey(Long refid,Long customerEntityId) {

        return abcDao.getByForeignKey(refid,customerEntityId);
    }

ABCDaoImpl.java

@Repository
public class ABCDaoImpl implements ABCDao {

    @Autowired
    @Qualifier("mySessionFactory1")
    protected SessionFactory sessionFactory;
    @SuppressWarnings("unchecked")
    @Override
    public ABC getByForeignKey(Long refId,Long customerEntityId) {
        Query query = sessionFactory.getCurrentSession().createQuery(" from ABC where RefId = " + refId + " and customerEntityId = "+customerEntityId);
        List<ABC> abcs = query.list();
        if(abcs.size()>0) {
            return abcs.get(0);
        }

        return null;
    }
}

这里我试图访问new_db中的值,它始终返回null值, 任何帮助将受到高度赞赏。我正在处理这个过去2天但没有找到任何解决方案,请告诉我是否需要更多的片段。

0 个答案:

没有答案