我使用Spring和Hibernate的彈簧配置如下。我如何配置兩個數據源,會話工廠。交易使用註釋進行管理。請指教休眠配置多個數據源和多個會話工廠
<!-- we can use annotations -->
<context:annotation-config/>
<!-- package to look for annotated classes -->
<context:component-scan base-package="com.XXX.XXX.service.impl"/>
<!-- we will manage transactions with annotations -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- configure hibernate session factory -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
p:url="jdbc:sqlserver://DB_NAME\DB_INSTANCE:DB_PORT;databaseName=DB_NAME;username=DB_USER;password=DB_PASSWORD;" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="show_sql">false</prop>
</props>
</property>
</bean>
wot about without spring? – anshulkatta
請注意,SessionFactories/Datasource都將共享相同的事務管理器,除非您也限定它們。選中此鏈接(http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-multiple-tx-mgrs-with-attransactional) –