Ordinary hut

          人間一福地,勝似天仙宮
          posts - 61, comments - 50, trackbacks - 0, articles - 1
          spring配置ibatis的jdbc方式和proxool連接池方式,以sqlserver2005為例,驅(qū)動(dòng)為sqljdbc.jar
          jdbc.properties中內(nèi)容如下
          jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
          jdbc
          .url=jdbc\:sqlserver\://localhost\:1433; DatabaseName\=IBATIS
          jdbc
          .username=sa
          jdbc
          .password=123456
          1 直接用jdbc方式(這種方式適合開發(fā)階段,發(fā)布的程序強(qiáng)烈要求用連接池),代碼如下,這個(gè)都知道,沒什么可說的
          <?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:aop
          ="http://www.springframework.org/schema/aop"
              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-2.5.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
          >
              
              
          <context:component-scan base-package="com.mydomain">
                  
          <context:include-filter type="aspectj" expression="com.mydomain.spring..*"/>
              
          </context:component-scan>
              
          <context:property-placeholder location="classpath:jdbc.properties" />
              
          <bean id="dataSource" 
                  class
          ="org.apache.commons.dbcp.BasicDataSource"
                  destroy-method
          ="close">
                  
          <property name="driverClassName" value="${jdbc.driverClassName}" />
                  
          <property name="url" value="${jdbc.url}" />
                  
          <property name="username" value="${jdbc.username}" />
                  
          <property name="password" value="${jdbc.password}" />
              
          </bean>

              
          <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                  
          <property name="configLocation" value="classpath:com/mydomain/data/SqlMapConfig.xml"/>
                  
          <property name="dataSource" ref="dataSource"/>
              
          </bean>
              
          <!--事務(wù)管理直接用的DataSourceTransactionManager-->
              
          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                  
          <property name="dataSource" ref="dataSource"/>
              
          </bean>
              
          <aop:config>
                  
          <aop:pointcut id="baseServiceMethods"
                      expression
          ="execution(* com.mydomain.spring.*.*(..))" />
                  
          <aop:advisor advice-ref="txAdvice"
                      pointcut-ref
          ="baseServiceMethods" />
              
          </aop:config>
              
          <aop:aspectj-autoproxy />
              
          <tx:advice id="txAdvice" transaction-manager="txManager">
                  
          <tx:attributes>
                      
          <tx:method name="select*" read-only="true" propagation="REQUIRED"/>
                      
          <tx:method name="find*" read-only="true"  propagation="REQUIRED"/>
                      
          <tx:method name="save*"  propagation="REQUIRED" isolation="REPEATABLE_READ"/>
                      
          <tx:method name="update*"  propagation="REQUIRED" isolation="REPEATABLE_READ"/>
                      
          <tx:method name="add*"  propagation="REQUIRED" isolation="REPEATABLE_READ" />
                      
          <tx:method name="delete*"  propagation="REQUIRED" isolation="REPEATABLE_READ"/>
                  
          </tx:attributes>
              
          </tx:advice>
          </beans>
          SqlMapConfig.xml內(nèi)容如下:
          <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
              "http://www.ibatis.com/dtd/sql-map-config-2.dtd"
          >

          <sqlMapConfig>
            
          <sqlMap resource="com/mydomain/data/Account.xml"/>
            
          </sqlMapConfig>
          2 用proxool連接池方式,只有datasource發(fā)生變化,其他的無變動(dòng)
            需要加入proxool.jar
              <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
                  
          <property name="alias" value="test"></property>
                  
          <property name="delegateProperties">
                      
          <value>user=${jdbc.username},password=${jdbc.password}</value>
                  
          </property>
                  
          <property name="user" value="${jdbc.username}"/>
                  
          <property name="password" value="${jdbc.password}"/>
                  
          <property name="driver" value="${jdbc.driverClassName}"/>
                  
          <property name="driverUrl" value="${jdbc.url}"/>
                  
          <property name="houseKeepingTestSql" value="select CURRENT_DATE"></property>
                  <!--此處繼續(xù)增加proxool屬性,詳細(xì)見proxool文檔-->
              
          </bean>
          此處說明一下:屬性中的user和password不起任何作用,需要用delegateProperties方式寫一下,否則會(huì)報(bào)錯(cuò)誤,如下
              org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection.
          但是user和password還不能被去掉。

          除了上面的用delegateProperties之外,還可以將用戶名和密碼直接寫在url后面。

          proxool屬性的說明,在這里寫的比較詳細(xì)
          http://www.cnblogs.com/wllyy189/archive/2008/10/15/1311560.html
          3 c3p0方式
            需要加入c3p0.jar
              <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                  
          <property name="driverClass">
                          
          <value>${jdbc.driverClassName}</value>
                      
          </property>
                      
          <property name="jdbcUrl">
                          
          <value>${jdbc.url}</value>
                      
          </property>
                      
          <property name="user">
                          
          <value>${jdbc.username}</value>
                      
          </property>
                      
          <property name="password">
                          
          <value>${jdbc.password}</value>
                      
          </property>
                      <!--此處繼續(xù)增加c3p0屬性-->
              </bean>
          關(guān)于c3p0的屬性說明請(qǐng)參見:
          http://www.aygfsteel.com/Alpha/archive/2009/03/29/262789.html

          Feedback

          # re: spring配置ibatis的jdbc方式和proxool、c3p0連接池方式  回復(fù)  更多評(píng)論   

          2009-11-12 17:11 by treesky
          幫了很大忙!
          主站蜘蛛池模板: 梧州市| 安达市| 嫩江县| 古丈县| 崇信县| 延川县| 平遥县| 荥经县| 察隅县| 民勤县| 西华县| 马鞍山市| 扬中市| 合江县| 陈巴尔虎旗| 晋江市| 内乡县| 成安县| 商水县| 南宫市| 内丘县| 手机| 建德市| 衡山县| 民县| 上高县| 孟州市| 犍为县| 恩施市| 乌拉特后旗| 烟台市| 邯郸县| 清涧县| 余干县| 阿拉善右旗| 屯门区| 石阡县| 方山县| 屯昌县| 临沂市| 资阳市|