Ordinary hut

          人間一福地,勝似天仙宮
          posts - 61, comments - 50, trackbacks - 0, articles - 1

          spring+hibernate的幾種數據庫連接配置方式

          Posted on 2009-07-02 16:57 landor 閱讀(3870) 評論(0)  編輯  收藏 所屬分類: hibernate
          1 普通jdbc方式,基本代碼如下
          <?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:annotation-config/>
              
              
          <context:component-scan base-package="com.huaxia.oaapp">
                  
          <context:include-filter type="aspectj" expression="com.huaxia.oaapp.service..*"/>
                  
          <context:include-filter type="aspectj" expression="com.huaxia.oaapp.entity..*"/>
                  
          <context:exclude-filter type="aspectj" expression="com.huaxia.oaapp.action..*"/>
              
          </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="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="dataSource">
                      
          <ref bean="dataSource" />
                  
          </property>
                  
          <property name="hibernateProperties">
                      
          <props>
                          
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                          
          <prop key="hibernate.show_sql">true</prop>
                          
          <prop key="hiberante.format_sql">true</prop>
                      
          </props>
                  
          </property>
                  
          <property name="annotatedClasses">
                      
          <list>
                          
          <value>com.huaxia.oaapp.entity.User</value>
                      
          </list>
                  
          </property>
                  
          <property name="annotatedPackages">
                      
          <list>
                          
          <value>com.huaxia.oaapp.entity</value>
                      
          </list>
                  
          </property>
              
          </bean>

              
          <bean id="transactionManager"
                  class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
                  
          <property name="sessionFactory" ref="sessionFactory" />
              
          </bean>
              
          <aop:config>
                  
          <aop:pointcut id="baseServiceMethods"
                      expression
          ="execution(* com.huaxia.oaapp.service.*.*(..)),execution(* com.huaxia.oaapp.aop.*.*(..))" />
                  
          <aop:advisor advice-ref="txAdvice"
                      pointcut-ref
          ="baseServiceMethods" />
              
          </aop:config>
              
          <aop:aspectj-autoproxy />
              
          <tx:advice id="txAdvice" transaction-manager="transactionManager">
                  
          <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>
          上面hibernateProperties還可以這么配置,效果是一樣的
                  <property name="hibernateProperties">
                      
          <value>
                          hibernate.dialect=org.hibernate.dialect.SQLServerDialect
                          hibernate.show_sql=true
                          hiberante.format_sql=true
                      
          </value>
                  
          </property>

          還可以直接把配置信息放到hibernate.cfg.xml中,代碼如下
              <bean id="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="configLocations">
                      
          <list>
                          
          <value>classpath:hibernate.cfg.xml</value>
                      
          </list>
                  
          </property> 
              
          </bean>
          hibernate.cfg.xml代碼如下
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC
                  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >
          <hibernate-configuration>
              
          <session-factory name="sessionFactory">
                  
          <property name="hibernate.connection.driver_class">
                      com.microsoft.sqlserver.jdbc.SQLServerDriver
                  
          </property>
                  
          <property name="hibernate.connection.password">123456</property>
                  
          <property name="hibernate.connection.url">
                      jdbc:sqlserver://localhost:1433; DatabaseName=oadb
                  
          </property>
                  
          <property name="hibernate.connection.username">sa</property>
                  
          <property name="hibernate.dialect">
                      org.hibernate.dialect.SQLServerDialect
                  
          </property>
                  
          <property name="hibernate.show_sql">true</property>
                  
          <property name="hiberante.format_sql">true</property>
                  
          <property name="hibernate.cache.provider_class">
                      org.hibernate.cache.EhCacheProvider
                  
          </property>
                  
                  
          <property name="hibernate.connection.autocommit">false</property>
                  
          <mapping class="com.huaxia.oaapp.entity.User" />
              
          </session-factory>
          </hibernate-configuration>

          2 配置proxool連接池
          第一種:spring中配置proxool非常簡單,代碼如下
          <bean id="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="hibernateProperties">
                      
          <props>
                          
          <prop key="hibernate.dialect">
                              org.hibernate.dialect.SQLServerDialect
                          
          </prop>
                          
          <prop key="hibernate.show_sql">true</prop>
                          
          <prop key="hiberante.format_sql">true</prop>
                          
          <prop key="hibernate.proxool.pool_alias">myDataSource</prop>
                          
          <prop key="hibernate.proxool.xml">Proxool.xml</prop>
                      
          </props>
                  
          </property>
                  
              
          </bean>
          其中Proxool.xml在根目錄下,大致內容如下
          <something-else-entirely>
            
          <proxool>
              
          <alias>myDataSource</alias>
              
          <driver-url>
                  jdbc:sqlserver://localhost:1433; DatabaseName=test
                 
          </driver-url>
              
          <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
              
          <driver-properties>
                
          <property name="user" value="sa"/>
                
          <property name="password" value="123456"/>
              
          </driver-properties>
              
          <house-keeping-sleep-time>1000</house-keeping-sleep-time> 
              
          <maximum-connection-count>2</maximum-connection-count>
              
          <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
            
          </proxool>
          </something-else-entirely>
          第二種:上面是嵌入了proxool.xml文件,其實也可以直接嵌入proxool屬性,這樣就不用再建立Proxool.xml文件了,代碼如下
              <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}"/>
              
          </bean>
              
          <bean id="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="dataSource">
                      
          <ref bean="dataSource" />
                  
          </property>
                  
          <property name="hibernateProperties">
                      
          <props>
                          
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                          
          <prop key="hibernate.show_sql">true</prop>
                          
          <prop key="hiberante.format_sql">true</prop>
                      
          </props>
                  
          </property>
                  
              
          </bean>
          此處說明一下:屬性中的user和password不起任何作用,需要用delegateProperties方式寫一下,否則會報錯誤,如下
              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屬性的說明,在這里寫的比較詳細
          http://www.cnblogs.com/wllyy189/archive/2008/10/15/1311560.html

          第三種:在hibernate.cfg.xml中配置proxool連接池,代碼如下
          applicationContext.xml文件大致配置如下:
              <bean id="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

                  
          <property name="configLocations">
                      
          <list>
                          
          <value>classpath:hibernate.cfg.xml</value>
                      
          </list>
                  
          </property>
              
          </bean>
          hibernate.cfg.xml的配置如下:
          <hibernate-configuration>
               
          <session-factory name="sessionFactory">
                  
          <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
                  
          <property name="show_sql">true</property>
                  
          <property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
                  
          <property name="proxool.pool_alias">myDataSource</property>
                  
          <property name="proxool.xml">Proxool.xml</property>
                  
          <property name="connection.autocommit">=true </property>
                  
          <property name="jdbc.batch_size">20</property>
                  
          <property name="default_schema">dbo</property>
                  
          <mapping class="com.huaxia.oaapp.entity.User" />
                  
          <mapping class="com.huaxia.oaapp.entity.Document" />
                  
          <mapping class="com.huaxia.oaapp.entity.Certificate" />
              
          </session-factory>
          </hibernate-configuration>   
          其中Proxool.xml就是要加入的proxool連接池的配置文件
          主站蜘蛛池模板: 天津市| 济南市| 若尔盖县| 屏南县| 彭泽县| 石景山区| 潢川县| 长宁区| 灌云县| 溧阳市| 沂南县| 洛宁县| 南靖县| 灵山县| 邵阳县| 巨鹿县| 阜平县| 金昌市| 化隆| 甘谷县| 精河县| 泸定县| 罗山县| 东丽区| 新野县| 定州市| 灌阳县| 上饶市| 永胜县| 新巴尔虎左旗| 乌兰县| 兴国县| 临猗县| 古田县| 阳西县| 临桂县| 岑溪市| 廉江市| 海伦市| 九台市| 松江区|