日期:2014-05-17  浏览次数:20708 次

ssh多数据库配置 怎么get session ?
本帖最后由 tigerwithwing 于 2012-12-04 22:41:33 编辑 struts2+spring3.0+hibernate3.3
我配置的是双数据源,我想在程序中执行hql和sql语句,要执行hql和sql语句的前提是先getSession()。
怎么去获取2个数据源的 getSession()呢?请教高手。有代码例子最好。多谢多谢!
我的spring 配置文件applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" 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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<import resource="MpssServiceContext.xml" />
<!--数据源一: mpsss数据源   -->
<bean id="mpssHibernateSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:/com/hncatv/orm/hibernate/MpssHibernate.cfg.xml">
</property>
</bean>
<!--数据源二: worker数据源   -->
<bean id="workerHibernateSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:/com/hncatv/orm/hibernate/WorkerHibernate.cfg.xml">
</property>
</bean>


<bean id="mpssHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mpssHibernateSessionFactory"></property>
</bean>
<bean id="workerHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="workerHibernateSessionFactory"></property>
</bean>

<bean id="mpssTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mpssHibernateSessionFactory" />
</bean>
<bean id="workerTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="workerHibernateSessionFactory" />
</bean>

<tx:advice id="mpssTxAdvice" transaction-manager="mpssTxManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<tx:advice id="workerTxAdvice" transaction-manager="workerTxManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />