2012-01-20 103 views
0

我有一個基於Spring和休眠的應用程序。使用休眠創建兩個連接器到數據庫

我的任務是建立在這個應用兩個連接到數據庫 - 一個連接器爲只讀和第二是讀,寫等

如何我的配置應該是什麼樣子。

現在在WEB-INF文件夾中,我有3個文件:

休眠上下文:

<?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:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      "> 

    <context:property-placeholder location="/WEB-INF/spring.properties" /> 

    <!-- Enable annotation style of managing transactions --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> 
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->       
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> 
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
       p:dataSource-ref="dataSource" 
       p:configLocation="${hibernate.config}" 
       p:packagesToScan="com.esb.scs"/> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
       destroy-method="close" 
       p:driverClass="${app.jdbc.driverClassName}" 
       p:jdbcUrl="${app.jdbc.url}" 
       p:user="${app.jdbc.username}" 
       p:password="${app.jdbc.password}" 
       p:acquireIncrement="5" 
       p:idleConnectionTestPeriod="60" 
       p:maxPoolSize="100" 
       p:maxStatements="50" 
       p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
       p:sessionFactory-ref="sessionFactory" /> 



</beans> 

的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <property name="show_sql">true</property> 
    <property name="hbm2ddl.auto">update</property> 
    </session-factory> 
</hibernate-configuration> 

spring.properties:

# database properties 
app.jdbc.driverClassName=com.mysql.jdbc.Driver 
app.jdbc.url=jdbc:mysql://localhost/database 
app.jdbc.username=user 
app.jdbc.password=password 

#hibernate properties 
hibernate.config=/WEB-INF/hibernate.cfg.xml 

我該如何創建在一個應用程序中連接兩個數據庫?

我的問題是becouse我有兩個數據庫與複製,其中一個僅用於讀取和謝勝利,是寫...

有了您的幫助,我已經創建的文件是這樣的:

<?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:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      "> 

    <context:property-placeholder location="/WEB-INF/spring.properties" /> 

    <!-- Enable annotation style of managing transactions --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <tx:annotation-driven transaction-manager="transactionManagerr" /> 

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> 
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->       
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> 
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
       p:dataSource-ref="dataSource" 
       p:configLocation="${hibernate.config}" 
       p:packagesToScan="com.esb.scs"/> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
       destroy-method="close" 
       p:driverClass="${app.jdbc.driverClassName}" 
       p:jdbcUrl="${app.jdbc.url}" 
       p:user="${app.jdbc.username}" 
       p:password="${app.jdbc.password}" 
       p:acquireIncrement="5" 
       p:idleConnectionTestPeriod="60" 
       p:maxPoolSize="100" 
       p:maxStatements="50" 
       p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
       p:sessionFactory-ref="sessionFactory" /> 



    <bean id="sessionFactoryr" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
       p:dataSource-ref="dataSourcer" 
       p:configLocation="${hibernate.config}" 
       p:packagesToScan="com.esb.scs"/> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSourcer" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
       destroy-method="close" 
       p:driverClass="${app.jdbc.driverClassName}" 
       p:jdbcUrl="${app.jdbc.url}" 
       p:user="${appr.jdbc.username}" 
       p:password="${appr.jdbc.password}" 
       p:acquireIncrement="5" 
       p:idleConnectionTestPeriod="60" 
       p:maxPoolSize="100" 
       p:maxStatements="50" 
       p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManagerr" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
       p:sessionFactory-ref="sessionFactoryr" /> 



</beans> 

,現在當我在服務:

@Resource(name="sessionFactory") 
    private SessionFactory sessionFactory; 

    @Resource(name="sessionFactoryr") 
    private SessionFactory sessionFactoryr; 

,當我試圖使查詢與sessionFactoryr

我得到錯誤:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

,但我有

<filter> 
     <filter-name>hibernateFilter</filter-name> 
     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
     </filter> 
     <filter-mapping> 
     <filter-name>hibernateFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
     </filter-mapping> 

在web.xml

+0

你爲什麼要這麼做?可以讀取和寫入的連接也可以讀取。你不需要那樣做。哦,順便說一句,你的帖子中沒有問題。 –

+0

我需要它,因爲我有2個數據庫與複製,有一個創建只讀和第二個寫。 – Ilkar

+0

然後在你的問題中提到這個重要的事實。 –

回答

1

要建立兩個事務管理器,剛剛宣佈他們在你的應用程序方面,我使用一個屬性文件並讀入連接細節。我訂了該道具文件的訪問類型休眠的(也限制了數據庫服務器的權限):

<bean id="sessionFactoryOne" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
    p:dataSource-ref="dataSource" p:configLocation="WEB-INF/classes/hibernate.cfg.xml" 
    p:packagesToScan="com.mycompany" /> 

    <bean id="dataSourceONE" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
    destroy-method="close" p:driverClass="${app.jdbc.driverClassName}" 
    p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}" 
    p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="10" 
    p:maxStatements="50" p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
     p:sessionFactory-ref="sessionFactory"> 

<bean id="sessionFactoryTWO" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
      p:dataSource-ref="dataSourceTWO" 
      p:configLocation="WEB-INF/classes/hibernateTWO.cfg.xml" 
      p:packagesToScan="com.mycompany"/> 

<!-- Declare a datasource that has pooling capabilities--> 
<bean id="dataSourceTWO" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
      destroy-method="close" 
      p:driverClass="${two.jdbc.driverClassName}" 
      p:jdbcUrl="${two.jdbc.url}" 
      p:user="${two.jdbc.username}" 
      p:password="${two.jdbc.password}" 
      p:acquireIncrement="2" 
      p:idleConnectionTestPeriod="60" 
      p:maxPoolSize="5" 
      p:maxStatements="50" 
      p:minPoolSize="1" /> 

<!-- Declare a second transaction manager--> 
<bean id="transactionManagerTWO" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
     p:sessionFactory-ref="sessionFactoryTWO"> 
     <qualifier value="Traveller"/>    

,然後你可以參考一下他們​​像在年服務層像這樣:

@Resource(name = "sessionFactoryTWO") 
private SessionFactory sessionFactoryTWO; 

@Resource(name = "sessionFactory") 
private SessionFactory sessionFactory; 

然後對於您的服務層中的方法,您將擁有已經使交易的方法:

@Transactional(readOnly = true) 
public void myReadOnlyMethod(Whatever whatever) 

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) 
public void myWriteMethod(Whatever whatever) 
+0

感謝年輕人的幫助,我喜歡你說的,並且存在一個問題 - 沒有Hibernate Session綁定到線程,並且配置不允許創建非事務性的 - 請參閱upper – Ilkar

+0

我假定您有註釋正確工作。 ..你需要使每個方法交易(更新答案) – NimChimpsky