spring配置数据库连接池(SSH)

 

 

1db.propertities 数据库连接参数配置文件:

#oracle配置

#driver=oracle.jdbc.driver.OracleDriver

#连接url

#url=jdbc:oracle:thin:@127.0.0.1:1521:xe

#用户名

#user=system

#密码

#password=oracle

 

#mysql配置

#驱动名    格式    key=value    注意不要有空格不要结尾分号

driver=com.mysql.jdbc.Driver

#连接url

url=jdbc:mysql://localhost:3306/utf8?useUnicode=true&characterEncoding=utf-8

#用户名

user=root

#密码

password=1234

 

#连接池配置

initialPoolSize=2

maxPoolSize=6

minPoolSize=2

maxIdleTime=600

acquireIncrement=2

timeBetweenEvictionRunsMillis=600

minEvictableIdleTimeMillis=400

 

 

2spring_hibernate.xml 获取数据库源(数据库连接池)的配置文件:

<?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:context="http://www.springframework.org/schema/context"  xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"  xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:util="http://www.springframework.org/schema/util"  xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa  http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.2.xsd">

        

         <!-- 配置 spring hibernate 整合信息 -->

         <util:properties id="props" location="classpath:com/chinasofti/etc/conf/db.properties"></util:properties>

        

<!-- 数据源配置 -->

         <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

      <!-- 基本属性 urluserpassword -->

      <property name="url" value="#{props.url}" />

      <property name="username" value="#{props.user}" />

      <property name="password" value="#{props.password}" />

     

<!-- 配置初始化大小、最小、最大 -->

      <property name="initialSize" value="#{props.initialPoolSize}" />

      <property name="minIdle" value="#{props.minPoolSize}" />

      <property name="maxActive" value="#{props.maxPoolSize}" />

     

<!-- 配置获取连接等待超时的时间 -->

      <property name="maxWait" value="#{props.maxIdleTime}" />

    

 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

      <property name="timeBetweenEvictionRunsMillis" value="#{props.timeBetweenEvictionRunsMillis}" />

    

 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

      <property name="minEvictableIdleTimeMillis" value="#{props.minEvictableIdleTimeMillis}" />

    

 <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->

      <property name="poolPreparedStatements" value="true" />

      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

  </bean>

 

         <!-- sessionFactory  的加载:

                            1.可以直接在当前spring的配置中通过属性注入方式来配置  该对象

                            2.也可以通过读取hibernate.cfg.xml配置文件来实现属性注入

              -->

         <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

                   <property name="dataSource" ref="ds"></property>

                   <!-- <property name="hibernateProperties"></property> -->

                   <property name="configLocation" value="classpath:com/chinasofti/etc/conf/hibernate.cfg.xml"></property>

         </bean>

         <!-- 创建HibernateTemplate对象 spring提供的用来操作数据库的对象,封装了hibernatesession的功能 -->

         <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">

                   <property name="sessionFactory" ref="sessionFactory"></property>

         </bean>

        

        

         <!-- 开启事务注解扫描 -->

         <!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/> -->

         <!-- 声明事务管理组件 -->

         <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

                   <property name="sessionFactory" ref="sessionFactory"></property>

         </bean>

        

        

         <!-- 声明事务的类型和范围 -->

         <tx:advice id="txAdvice" transaction-manager="txManager">

                   <tx:attributes>

                   <!-- 设置事务通知作用的范围和类型     以方法为单位来进行设置 -->

                   <!--name="add*" 表示给add开头的方法设置事务通知 

                                          propagation="REQUIRED"  传播策略  "REQUIRED"  该方法必须在一个事务中,如果没有事务则新建事务

                                          如果已经在一个事务中,则加入该事务

                                          read-only="false"   事务的类型   read-only表示只读事务    

                                          isolation="DEFAULT"  事务的隔离级别  DEFAULT为遵循数据库自动维护的事务隔离级别  

                            -->

                            <tx:method name="add*"  propagation="REQUIRED" read-only="false"

                            isolation="DEFAULT" rollback-for="java.lang.ClassNotFoundException"/>

                            <tx:method name="update*" propagation="REQUIRED" read-only="false"/>

                            <tx:method name="del*" propagation="REQUIRED" read-only="false"/>

                            <tx:method name="find*"  read-only="true"/>

                            <tx:method name="count*"  read-only="true"/>

                            <tx:method name="execute*"  propagation="REQUIRED" read-only="false"/>

                   </tx:attributes>

         </tx:advice>

         <!-- 设置切面 -->

         <aop:config proxy-target-class="true">

                   <aop:advisor advice-ref="txAdvice" pointcut="within(com.chinasofti.etc.action..*)"/>

         </aop:config>

</beans>

posted @ 2017-04-07 20:32  colily  阅读(2728)  评论(0)    收藏  举报