各种数据源配置之Spring+JPA配置BoneCP数据源

beans.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:aop="http://www.springframework.org/schema/aop"
       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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <aop:aspectj-autoproxy/>
   
    <context:component-scan base-package="com.huhui"/><!-- 扫描注解方式 -->
   
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value=" huhui"/>
    </bean>
   
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
  
   <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

web.xml代码:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
</context-param>
其它的略

persistence.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name=" huhui" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/huhui?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>

<property name="hibernate.connection.provider_class" value="com.jolbox.bonecp.provider.BoneCPConnectionProvider"/>
<property name="hibernate.connection.provider_class" value="com.jolbox.bonecp.provider.BoneCPConnectionProvider"/>
<!-- 设置连接的空闲存活时间,默认为60,单位:分钟 -->
<property name="bonecp.idleMaxAge" value="240"/>
<!-- 设置测试connection的间隔时间。这个参数默认为240,单位:分钟 -->
<property name="bonecp.idleConnectionTestPeriod" value="60"/>
<!-- 设置分区个数。这个参数默认为1,建议3-4(根据特定应用程序而定) -->
<property name="bonecp.partitionCount" value="3"/>
<!-- 设置分区中的连接增长数量。这个参数默认为1 -->
<property name="bonecp.acquireIncrement" value="10"/>
<!-- 设置每个分区含有连接最大个数。这个参数默认为2 -->
<property name="bonecp.maxConnectionsPerPartition" value="300"/>
<!-- 设置每个分区含有连接最大小个数。这个参数默认为0 -->
<property name="bonecp.minConnectionsPerPartition" value="20"/>
<!-- 设置statement缓存个数。这个参数默认为0 -->
<property name="bonecp.statementsCacheSize" value="50"/>
<!-- 设置连接助手线程个数。这个参数默认为3 -->
<property name="bonecp.releaseHelperThreads" value="3"/> </properties>
</persistence-unit>
</persistence>

配置数据源时,我个人不喜欢配置多个数据源。以上使用的数据源是BoneCP数据源,性能比C3P0好,当然也不是说不用C3P0了。下次我会给出C3P0的配置代码及其Jar包

猜你喜欢

转载自javahuhui.iteye.com/blog/1456960