我需要在我的项目中使用hibernate连接多个数据库。我尝试使用2个数据源bean标签,但它不工作它会引发错误。但是,如果我给1个数据源bean,它工作正常。在这里,我展示了我的属性文件和servlet.xml文件。
database.properties:
'homeUrl' => '/yii2advance',
'components' => [
'request' => [
'baseUrl' => '/yii2advance', // localhost/yii2advance
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
弹簧servlet.xml中:
#mySQL DB Properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/abc
database.user=root
database.password=pass
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
#DB2DB Properties
db2.driver=com.ibm.db2.jcc.DB2Driver
db2.url=jdbc:db2://123.141.74.323:50000/DB
db2.user=user
db2.password=pass
db2hibernate.dialect=org.hibernate.dialect.DB2Dialect
db2hibernate.show_sql=true
db2hibernate.hbm2ddl.auto=update
如果我评论第二个数据源bean意味着它工作正常,但我没有注释第二个数据源bean意味着它会抛出错误。下面我显示错误。
错误:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--Properties file -->
<context:property-placeholder location="classpath:resources/database.properties" />
<!--Packages to scan -->
<context:component-scan base-package="com.gcs.*" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<tx:annotation-driven transaction-manager="hibernateTransactionManager1"/>
<!--View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<!--End of View Resolver -->
<!-- mySQL DataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.gcs.beans.SystemBean</value>
<value>com.gcs.beans.TemplateBean</value>
<value>com.gcs.beans.LoginBean</value>
<value>com.gcs.beans.TestSuitBean</value>
</list>
</property>
<property name="hibernateProperties">
<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 id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- End of mySQL DataSource -->
<!-- DB2 DataSource -->
<bean id="DB2dataSource" class="com.ibm.db2.jcc.DB2DataSource">
<property name="driverClassName" value="${db2.driver}" />
<property name="url" value="${db2.url}" />
<property name="username" value="${db2.user}" />
<property name="password" value="${db2.password}" />
</bean>
<bean id="DB2sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="DB2dataSource" />
<property name="annotatedClasses">
<list>
<value>com.gcs.beans.AudTrailBean</value>
<value>com.gcs.beans.AudTrailDtlBean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${db2hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${db2hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${db2hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager1"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="DB2sessionFactory" />
</bean>
<!-- End of DB2 DataSource -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10000000" />
</bean>
</beans>
我不知道自己弄错了什么。我希望有人能帮助我找到错误。
答案 0 :(得分:1)
你的spring-servlet.xml看起来很好。当您尝试注入sessionFactory时,您会收到错误。请尝试以下代码。
@Transactional(value ="hibernateTransactionManager",propagation = Propagation.REQUIRED)
public class FirstDao extends HibernateDaoSupport{
@Autowired
public void init(@Qualifier("sessionFactory1") SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
public SampleEstimate fetchEstimate(long objectId){
SampleEstimate estimate=(SampleEstimate) getSessionFactory().openSession().getNamedQuery("SampleEstimate.findOne").
setLong("objectId",objectId).list().get(0);
return estimate;
}
public void saveGroup(SampleEstimateGroup group){
getHibernateTemplate().saveOrUpdate(group);
}
}
第二届工厂注入另一个Dao
@Transactional(value = "hibernateTransactionManager1",propagation = Propagation.REQUIRED,readOnly = false)
public class SecondDao extends HibernateDaoSupport {
@Autowired
@Qualifier("secondSessionFactory")
public void init(SessionFactory sessionFactory){
setSessionFactory(sessionFactory) ;
}
public SecondDbEntity getSecondDbEntity(){
return (SecondDbEntity)getHibernateTemplate().findByNamedQuery("SecondDbEntity.findAll").get(0);
}
}
希望这会有所帮助..