Ordinary hut

人间一福地,胜似天仙宫
posts - 61, comments - 50, trackbacks - 0, articles - 1

spring+hibernate的几种数据库连接配置方式

Posted on 2009-07-02 16:57 landor 阅读(3788) 评论(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连接池的配置文件

只有注册用户登录后才能发表评论。


网站导航: