最近做了一個項目,中間要用到mysql和oracle兩種資料庫.這就要求在Hibernate中同時配置兩個資料源.
這是我第一次遇到這個問題,是以寫點資料記錄配置過程.
做過SSH架構開發的人都知道,Spring包裝Hibernate的話所有的基本配置都寫在applicationContext.xml檔案中.
是以兩個資料源的配置也是寫在這個檔案中.
我們要做的就是在裡面寫入這樣的配置:
第一個資料源:
<bean id="資料源1"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="驅動類">
</property>
<property name="url"
value="資料位址">
</property>
<property name="username" value="使用者名"></property>
<property name="password" value="密碼"></property>
</bean>
<bean id="資料工廠1"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="資料源1" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>表的配置檔案位址</value>
</list>
</property>
</bean>
第二個資料源:
<bean id="資料源2"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="驅動類">
</property>
<property name="url"
value="資料位址">
</property>
<property name="username" value="使用者名"></property>
<property name="password" value="密碼"></property>
</bean>
<bean id="資料工廠2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="資料源2" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>表的配置檔案位址</value>
</list>
</property>
</bean>
當然你的事務封裝也的寫兩種配置,最後你使用IOP思想向DAO中注入資料源的時候選擇一下就可以了.
本來我也是向上面這樣配置的.不過我發現了一個問題.
頁面總是出現這樣的錯誤提示(如下):
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:360)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:686)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:219)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:149)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:541)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:223)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:207)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:146)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
com.by.yy.filter.MyFilter.doFilter(MyFilter.java:32)
好像是找不到SessionFactory這個bean.
如果遇到這樣的情況有兩種解決方式:
1.檢查你是否有jta.jar這個檔案
2.将上面的兩個資料源配置改為:
第一個資料源:
<bean id="資料源1"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="驅動類">
</property>
<property name="url"
value="資料位址">
</property>
<property name="username" value="使用者名"></property>
<property name="password" value="密碼"></property>
</bean>
<bean id="資料工廠1" name="别名"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="資料源1" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>表的配置檔案位址</value>
</list>
</property>
</bean>
第二個資料源:
<bean id="資料源2"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="驅動類">
</property>
<property name="url"
value="資料位址">
</property>
<property name="username" value="使用者名"></property>
<property name="password" value="密碼"></property>
</bean>
<bean id="資料工廠2" name="别名"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="資料源2" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>表的配置檔案位址</value>
</list>
</property>
</bean>
注意兩個資料工廠的别名不能一樣.而且其中一個必須是sessionFactory. 因為他需要一個預設的sessionFactory
到此為止兩個資料源就算配置好了..有可能遇到的環境不一樣,解決方法不一樣.以上所寫,隻是個人見解.