spring配置oracle多個使用者的datasource
阿新 • • 發佈:2019-02-07
一個oracle下的兩個使用者,App和Fc。程式對Fc只有select操作,不涉及事務。App為一般應用,要求有事務。
- <beanid="dataSourceApp"
- class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <propertyname="driverClass"
- value="oracle.jdbc.driver.OracleDriver">
- </property>
- <propertyname="jdbcUrl"
- value="jdbc:oracle:thin:@localhost:1521:oranb">
- </property>
- <propertyname="user"value="stmg"></property>
- <propertyname="password"value="stmg"></property>
- <propertyname="minPoolSize">
- <value>1</value>
- </
- <propertyname="maxPoolSize">
- <value>10</value>
- </property>
- <propertyname="initialPoolSize">
- <value>10</value>
- </property>
- <propertyname="maxIdleTime">
- <value>30</value
- </property>
- <propertyname="acquireIncrement">
- <value>5</value>
- </property>
- <propertyname="maxStatements">
- <value>0</value>
- </property>
- <propertyname="idleConnectionTestPeriod">
- <value>60</value>
- </property>
- <propertyname="acquireRetryAttempts">
- <value>30</value>
- </property>
- <propertyname="breakAfterAcquireFailure">
- <value>true</value>
- </property>
- <propertyname="testConnectionOnCheckout">
- <value>false</value>
- </property>
- </bean>
- <!-- C3P0 -->
- <beanid="dataSourceFc"
- class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <propertyname="driverClass"
- value="oracle.jdbc.driver.OracleDriver">
- </property>
- <propertyname="jdbcUrl"
- value="jdbc:oracle:thin:@localhost:1521:oranb">
- </property>
- <propertyname="user"value="etpdb"></property>
- <propertyname="password"value="etpu1234#"></property>
- <propertyname="minPoolSize">
- <value>1</value>
- </property>
- <propertyname="maxPoolSize">
- <value>10</value>
- </property>
- <propertyname="initialPoolSize">
- <value>10</value>
- </property>
- <propertyname="maxIdleTime">
- <value>30</value>
- </property>
- <propertyname="acquireIncrement">
- <value>5</value>
- </property>
- <propertyname="maxStatements">
- <value>0</value>
- </property>
- <propertyname="idleConnectionTestPeriod">
- <value>60</value>
- </property>
- <propertyname="acquireRetryAttempts">
- <value>30</value>
- </property>
- <propertyname="breakAfterAcquireFailure">
- <value>true</value>
- </property>
- <propertyname="testConnectionOnCheckout">
- <value>false</value>
- </property>
- </bean>
- <!-- sessionFactory App -->
- <beanid="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource">
- <refbean="dataSourceApp"/>
- </property>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- <!-- 允許自動提交 -->
- <propkey="hibernate.connection.autocommit">false</prop>
- <!-- 顯示sql語句 -->
- <propkey="hibernate.show_sql">true</prop>
- </props>
- </property>
- <propertyname="mappingResources">
- <list>
- <value>com/mutildatasource/po/Iasolution.hbm.xml</value>
- <value>com/mutildatasource/po/Iaquestion.hbm.xml</value>
- </list>
- </property>
- </bean>
- <!-- sessionFactory Fc -->
- <beanid="sessionFactoryFc"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource">
- <refbean="dataSourceFc"/>
- </property>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- <!-- 允許自動提交 -->
- <propkey="hibernate.connection.autocommit">false</prop>
- <!-- 顯示sql語句 -->
- <propkey="hibernate.show_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- App hibernateTemplate -->
- <beanid="hibernateTemplate"
- class="org.springframework.orm.hibernate3.HibernateTemplate">
- <propertyname="sessionFactory">
- <refbean="sessionFactory"/>
- </property>
- </bean>
- <!-- Fc hibernateTemplate -->
- <beanid="hibernateTemplateFc"
- class="org.springframework.orm.hibernate3.HibernateTemplate">
- <propertyname="sessionFactory">
- <refbean="sessionFactoryFc"/>
- </property>
- </bean>
- <!-- 定義事務管理器,使用適用於Hibernte的事務管理器-->
- <beanid="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <!-- HibernateTransactionManager bean需要依賴注入一個SessionFactory bean的引用-->
- <propertyname="sessionFactory">
- <reflocal="sessionFactory"/>
- </property>
- </bean>
- <!-- 配置事務攔截器-->
- <beanid="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <!-- 事務攔截器bean需要依賴注入一個事務管理器 -->
- <propertyname="transactionManager"ref="transactionManager"/>
- <propertyname="transactionAttributes">
- <!-- 下面定義事務傳播屬性-->
- <props>
- <propkey="save*">PROPAGATION_REQUIRED</prop>
- <propkey="set*">PROPAGATION_REQUIRED</prop>
- <propkey="update*">PROPAGATION_REQUIRED</prop>
- <propkey="delete*">PROPAGATION_REQUIRED</prop>
- <propkey="sort*">PROPAGATION_REQUIRED</prop>
- <propkey="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 定義BeanNameAutoProxyCreator,該bean是個bean後處理器,無需被引用,因此沒有id屬性
- 這個bean後處理器,根據事務攔截器為目標bean自動建立事務代理 -->
- <bean
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <!-- 指定對滿足哪些bean name的bean自動生成業務代理 -->
- <propertyname="beanNames">
- <!-- 下面是所有需要自動建立事務代理的bean-->
- <list>
- <value>mutilDataSourceService</value>
- </list>
- <!-- 此處可增加其他需要自動建立事務代理的bean-->
- </property>
- <!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器-->
- <propertyname="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- <!-- 此處可增加其他新的Interceptor -->
- </list>
- </property>
- </bean>
- <beanid="appDao"class="com.mutildatasource.dao.impl.AppDaoImpl">
- <propertyname="hibernateTemplate">
- <refbean="hibernateTemplate"/>
- </property>
- </bean>
- <beanid="fcDao"class="com.mutildatasource.dao.impl.FcDaoImpl">
- <propertyname="hibernateTemplateFc">
- <refbean="hibernateTemplateFc"/>
- </property>
- <propertyname="hibernateTemplate">
- <refbean="hibernateTemplate"/>
- </property>
- </bean>
- <beanid="mutilDataSourceService"class="com.mutildatasource.service.impl.MutilDataSourceServiceImpl">
- <propertyname="appDao">
- <refbean="appDao"/>
- </property>
- <propertyname="fcDao">
- <refbean="fcDao"/>
- </property>
- </bean>
- <beanname="/datasource"
- class="com.mutildatasource.struts.action.MutilDataSourceAction">
- <propertyname="mutilDataSourceService">
- <refbean="mutilDataSourceService"/>
- </property>
- </bean>
service : updateAppData() 和 updateAppData2()之一發生異常,兩個都會回滾,updateFcData()則不會,完全和以上兩個無關(實際需求中只有select),也就是隻有updateAppData,updateAppData2是在一個事務中的。
- publicclass MutilDataSourceServiceImpl implements MutilDataSourceService {
- private AppDaoImpl appDao;
- private FcDaoImpl fcDao;
- public String getMutilDataSourceDataService(int answerid, int status) {
- String result = fcDao.updateFcData();
- appDao.updateAppData(answerid, status);
- appDao.updateAppData2(answerid, status);
- returnnull;
- }
- publicvoid setAppDao(AppDaoImpl appDao) {
- this.appDao = appDao;
- }
- publicvoid setFcDao(FcDaoImpl fcDao) {
- this.fcDao = fcDao;
- }
- }
dao
- publicclass FcDaoImpl extends HibernateDaoSupport implements FcDao {
- private HibernateTemplate hibernateTemplateFc;
- public String updateCaiHuiData() {
- final String sql = "update tbusinflag set c_type='1' where c_businflag='01'";
- return (String)hibernateTemplateFc.execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException {
- Query query = session.createSQLQuery(sql);
- query.executeUpdate();
- returnnull;
- }
- });
- }
- publicvoid setHibernateTemplateFc(HibernateTemplate hibernateTemplateFc) {
- this.hibernateTemplateFc = hibernateTemplateFc;
- }
- }
- publicclass AppDaoImpl extends HibernateDaoSupport implements AppDao{
- publicvoid updateAppData(finalint answerid, finalint status) {
- getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- String hql = "UPDATE Iaquestion SET stsatus=? WHERE answerid=?"; //異常
- Query q = session.createQuery(hql);
- q.setInteger(0, 888);
- q.setInteger(1, 29);
- q.executeUpdate();
- returnnull;
- }
- });
- }
- publicvoid updateAppData2(finalint solutionid, finalint status) {
- getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException {
- String hql = "UPDATE Iasolution SET status=? WHERE solutionid=?"; //正常
- // String hql = "UPDATE Iasolution SET statuss=? WHERE solutionid=?"; 異常
- Query q = session.createQuery(hql);
- q.setInteger(0, status);
- q.setInteger(1, solutionid);
- q.executeUpdate();
- returnnull;
- }
- });
- }
- }
配置了兩個sessionFactory不知道會不會有問題,Fc換成sqlserver不知道行不行,還有datasource換成JNDI,試試。
轉載地址:http://blog.csdn.net/digyso888/article/details/4402264