1. 程式人生 > >spring配置oracle多個使用者的datasource

spring配置oracle多個使用者的datasource

一個oracle下的兩個使用者,App和Fc。程式對Fc只有select操作,不涉及事務。App為一般應用,要求有事務。

  1. <beanid="dataSourceApp"
  2.         class="com.mchange.v2.c3p0.ComboPooledDataSource">
  3.         <propertyname="driverClass"
  4.             value="oracle.jdbc.driver.OracleDriver">
  5.         </property>
  6.         <propertyname="jdbcUrl"
  7.             value="jdbc:oracle:thin:@localhost:1521:oranb">
  8.         </property>
  9.         <propertyname="user"value="stmg"></property>
  10.         <propertyname="password"value="stmg"></property>
  11.         <propertyname="minPoolSize">
  12.             <value>1</value>
  13.         </
    property>
  14.         <propertyname="maxPoolSize">
  15.             <value>10</value>
  16.         </property>
  17.         <propertyname="initialPoolSize">
  18.             <value>10</value>
  19.         </property>
  20.         <propertyname="maxIdleTime">
  21.             <value>30</value
    >
  22.         </property>
  23.         <propertyname="acquireIncrement">
  24.             <value>5</value>
  25.         </property>
  26.         <propertyname="maxStatements">
  27.             <value>0</value>
  28.         </property>
  29.         <propertyname="idleConnectionTestPeriod">
  30.             <value>60</value>
  31.         </property>
  32.         <propertyname="acquireRetryAttempts">
  33.             <value>30</value>
  34.         </property>
  35.         <propertyname="breakAfterAcquireFailure">
  36.             <value>true</value>
  37.         </property>
  38.         <propertyname="testConnectionOnCheckout">
  39.             <value>false</value>
  40.         </property>
  41.     </bean>
  42.     <!-- C3P0 -->
  43.     <beanid="dataSourceFc"
  44.         class="com.mchange.v2.c3p0.ComboPooledDataSource">
  45.         <propertyname="driverClass"
  46.             value="oracle.jdbc.driver.OracleDriver">
  47.         </property>
  48.         <propertyname="jdbcUrl"
  49.             value="jdbc:oracle:thin:@localhost:1521:oranb">
  50.         </property>
  51.         <propertyname="user"value="etpdb"></property>
  52.         <propertyname="password"value="etpu1234#"></property>
  53.         <propertyname="minPoolSize">
  54.             <value>1</value>
  55.         </property>
  56.         <propertyname="maxPoolSize">
  57.             <value>10</value>
  58.         </property>
  59.         <propertyname="initialPoolSize">
  60.             <value>10</value>
  61.         </property>
  62.         <propertyname="maxIdleTime">
  63.             <value>30</value>
  64.         </property>
  65.         <propertyname="acquireIncrement">
  66.             <value>5</value>
  67.         </property>
  68.         <propertyname="maxStatements">
  69.             <value>0</value>
  70.         </property>
  71.         <propertyname="idleConnectionTestPeriod">
  72.             <value>60</value>
  73.         </property>
  74.         <propertyname="acquireRetryAttempts">
  75.             <value>30</value>
  76.         </property>
  77.         <propertyname="breakAfterAcquireFailure">
  78.             <value>true</value>
  79.         </property>
  80.         <propertyname="testConnectionOnCheckout">
  81.             <value>false</value>
  82.         </property>
  83.     </bean>
  84.     <!-- sessionFactory App -->
  85.     <beanid="sessionFactory"
  86.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  87.         <propertyname="dataSource">
  88.             <refbean="dataSourceApp"/>
  89.         </property>
  90.         <propertyname="hibernateProperties">
  91.             <props>
  92.                 <propkey="hibernate.dialect">
  93.                     org.hibernate.dialect.Oracle9Dialect  
  94.                 </prop>
  95.                 <!-- 允許自動提交 -->
  96.                 <propkey="hibernate.connection.autocommit">false</prop>
  97.                 <!-- 顯示sql語句 -->
  98.                 <propkey="hibernate.show_sql">true</prop>
  99.             </props>
  100.         </property>
  101.         <propertyname="mappingResources">
  102.             <list>
  103.                 <value>com/mutildatasource/po/Iasolution.hbm.xml</value>
  104.                 <value>com/mutildatasource/po/Iaquestion.hbm.xml</value>
  105.             </list>
  106.         </property>
  107.     </bean>
  108.     <!-- sessionFactory Fc -->
  109.     <beanid="sessionFactoryFc"
  110.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  111.         <propertyname="dataSource">
  112.             <refbean="dataSourceFc"/>
  113.         </property>
  114.         <propertyname="hibernateProperties">
  115.             <props>
  116.                 <propkey="hibernate.dialect">
  117.                     org.hibernate.dialect.Oracle9Dialect  
  118.                 </prop>
  119.                 <!-- 允許自動提交 -->
  120.                 <propkey="hibernate.connection.autocommit">false</prop>
  121.                 <!-- 顯示sql語句 -->
  122.                 <propkey="hibernate.show_sql">true</prop>
  123.             </props>
  124.         </property>
  125.     </bean>
  126.     <!-- App hibernateTemplate -->
  127.     <beanid="hibernateTemplate"
  128.         class="org.springframework.orm.hibernate3.HibernateTemplate">
  129.         <propertyname="sessionFactory">
  130.             <refbean="sessionFactory"/>
  131.         </property>
  132.     </bean>
  133.     <!-- Fc hibernateTemplate -->
  134.     <beanid="hibernateTemplateFc"
  135.         class="org.springframework.orm.hibernate3.HibernateTemplate">
  136.         <propertyname="sessionFactory">
  137.             <refbean="sessionFactoryFc"/>
  138.         </property>
  139.     </bean>
  140.     <!-- 定義事務管理器,使用適用於Hibernte的事務管理器-->
  141.     <beanid="transactionManager"
  142.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  143.         <!-- HibernateTransactionManager bean需要依賴注入一個SessionFactory bean的引用-->
  144.         <propertyname="sessionFactory">
  145.             <reflocal="sessionFactory"/>
  146.         </property>
  147.     </bean>
  148.     <!-- 配置事務攔截器-->
  149.     <beanid="transactionInterceptor"
  150.         class="org.springframework.transaction.interceptor.TransactionInterceptor">
  151.         <!-- 事務攔截器bean需要依賴注入一個事務管理器 -->
  152.         <propertyname="transactionManager"ref="transactionManager"/>
  153.         <propertyname="transactionAttributes">
  154.             <!-- 下面定義事務傳播屬性-->
  155.             <props>
  156.                 <propkey="save*">PROPAGATION_REQUIRED</prop>
  157.                 <propkey="set*">PROPAGATION_REQUIRED</prop>
  158.                 <propkey="update*">PROPAGATION_REQUIRED</prop>
  159.                 <propkey="delete*">PROPAGATION_REQUIRED</prop>
  160.                 <propkey="sort*">PROPAGATION_REQUIRED</prop>
  161.                 <propkey="*">PROPAGATION_REQUIRED</prop>
  162.             </props>
  163.         </property>
  164.     </bean>
  165.     <!-- 定義BeanNameAutoProxyCreator,該bean是個bean後處理器,無需被引用,因此沒有id屬性      
  166.         這個bean後處理器,根據事務攔截器為目標bean自動建立事務代理 -->
  167.     <bean
  168.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  169.         <!-- 指定對滿足哪些bean name的bean自動生成業務代理 -->
  170.         <propertyname="beanNames">
  171.             <!-- 下面是所有需要自動建立事務代理的bean-->
  172.             <list>
  173.                 <value>mutilDataSourceService</value>
  174.             </list>
  175.             <!-- 此處可增加其他需要自動建立事務代理的bean-->
  176.             </property>
  177.         <!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器-->
  178.         <propertyname="interceptorNames">
  179.             <list>
  180.                 <value>transactionInterceptor</value>
  181.                 <!-- 此處可增加其他新的Interceptor -->
  182.             </list>
  183.         </property>
  184.     </bean>
  1. <beanid="appDao"class="com.mutildatasource.dao.impl.AppDaoImpl">
  2.         <propertyname="hibernateTemplate">
  3.             <refbean="hibernateTemplate"/>
  4.         </property>
  5.     </bean>
  6.     <beanid="fcDao"class="com.mutildatasource.dao.impl.FcDaoImpl">
  7.         <propertyname="hibernateTemplateFc">
  8.             <refbean="hibernateTemplateFc"/>
  9.         </property>
  10.         <propertyname="hibernateTemplate">
  11.             <refbean="hibernateTemplate"/>
  12.         </property>
  13.     </bean>
  14.     <beanid="mutilDataSourceService"class="com.mutildatasource.service.impl.MutilDataSourceServiceImpl">
  15.         <propertyname="appDao">
  16.             <refbean="appDao"/>
  17.         </property>
  18.         <propertyname="fcDao">
  19.             <refbean="fcDao"/>
  20.         </property>
  21.     </bean>
  22.     <beanname="/datasource"
  23.         class="com.mutildatasource.struts.action.MutilDataSourceAction">
  24.         <propertyname="mutilDataSourceService">
  25.             <refbean="mutilDataSourceService"/>
  26.         </property>
  27.     </bean>

service : updateAppData() 和 updateAppData2()之一發生異常,兩個都會回滾,updateFcData()則不會,完全和以上兩個無關(實際需求中只有select),也就是隻有updateAppData,updateAppData2是在一個事務中的。

  1. publicclass MutilDataSourceServiceImpl implements MutilDataSourceService {  
  2.     private AppDaoImpl appDao;  
  3.     private FcDaoImpl fcDao;  
  4.     public String getMutilDataSourceDataService(int answerid, int status) {  
  5.         String result = fcDao.updateFcData();  
  6.         appDao.updateAppData(answerid, status);  
  7.         appDao.updateAppData2(answerid, status);  
  8.         returnnull;  
  9.     }  
  10.     publicvoid setAppDao(AppDaoImpl appDao) {  
  11.         this.appDao = appDao;  
  12.     }  
  13.     publicvoid setFcDao(FcDaoImpl fcDao) {  
  14.         this.fcDao = fcDao;  
  15.     }  
  16. }  

dao

  1. publicclass FcDaoImpl extends HibernateDaoSupport implements FcDao {  
  2.     private HibernateTemplate hibernateTemplateFc;  
  3.     public String updateCaiHuiData() {  
  4.         final String sql = "update tbusinflag set c_type='1' where c_businflag='01'";  
  5.         return (String)hibernateTemplateFc.execute(new HibernateCallback() {  
  6.             public Object doInHibernate(Session session) throws HibernateException {  
  7.                 Query query = session.createSQLQuery(sql);  
  8.                 query.executeUpdate();  
  9.                 returnnull;  
  10.             }  
  11.         });  
  12.     }  
  13.     publicvoid setHibernateTemplateFc(HibernateTemplate hibernateTemplateFc) {  
  14.         this.hibernateTemplateFc = hibernateTemplateFc;  
  15.     }  
  16. }  
  1. publicclass AppDaoImpl extends HibernateDaoSupport implements AppDao{  
  2.     publicvoid updateAppData(finalint answerid, finalint status) {  
  3.         getHibernateTemplate().execute(new HibernateCallback() {  
  4.             public Object doInHibernate(Session session)  
  5.                     throws HibernateException {  
  6.                 String hql = "UPDATE Iaquestion SET stsatus=? WHERE answerid=?";    //異常
  7.                 Query q = session.createQuery(hql);  
  8.                 q.setInteger(0888);  
  9.                 q.setInteger(129);  
  10.                 q.executeUpdate();  
  11.                 returnnull;  
  12.             }  
  13.         });  
  14.     }  
  15.     publicvoid updateAppData2(finalint solutionid, finalint status) {  
  16.         getHibernateTemplate().execute(new HibernateCallback() {  
  17.             public Object doInHibernate(Session session)  
  18.                     throws HibernateException {  
  19.                 String hql = "UPDATE Iasolution SET status=? WHERE solutionid=?";   //正常
  20.     //              String hql = "UPDATE Iasolution SET statuss=? WHERE solutionid=?"; 異常
  21.                 Query q = session.createQuery(hql);  
  22.                 q.setInteger(0, status);  
  23.                 q.setInteger(1, solutionid);  
  24.                 q.executeUpdate();  
  25.                 returnnull;  
  26.             }  
  27.         });  
  28.     }  
  29. }  

配置了兩個sessionFactory不知道會不會有問題,Fc換成sqlserver不知道行不行,還有datasource換成JNDI,試試。

轉載地址:http://blog.csdn.net/digyso888/article/details/4402264