各種數(shù)據(jù)源配置之Spring+JPA配置BoneCP數(shù)據(jù)源
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>
-
- <!-- 配置事務(wù)管理器 -->
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory"/>
- </bean>
-
- <tx:annotation-driven transaction-manager="transactionManager"/>
- </beans>
復(fù)制代碼 web.xml代碼:- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
復(fù)制代碼 其它的略
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&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"/>
- <!-- 設(shè)置連接的空閑存活時(shí)間,默認(rèn)為60,單位:分鐘 -->
- <property name="bonecp.idleMaxAge" value="240"/>
- <!-- 設(shè)置測試connection的間隔時(shí)間。這個(gè)參數(shù)默認(rèn)為240,單位:分鐘 -->
- <property name="bonecp.idleConnectionTestPeriod" value="60"/>
- <!-- 設(shè)置分區(qū)個(gè)數(shù)。這個(gè)參數(shù)默認(rèn)為1,建議3-4(根據(jù)特定應(yīng)用程序而定) -->
- <property name="bonecp.partitionCount" value="3"/>
- <!-- 設(shè)置分區(qū)中的連接增長數(shù)量。這個(gè)參數(shù)默認(rèn)為1 -->
- <property name="bonecp.acquireIncrement" value="10"/>
- <!-- 設(shè)置每個(gè)分區(qū)含有連接最大個(gè)數(shù)。這個(gè)參數(shù)默認(rèn)為2 -->
- <property name="bonecp.maxConnectionsPerPartition" value="300"/>
- <!-- 設(shè)置每個(gè)分區(qū)含有連接最大小個(gè)數(shù)。這個(gè)參數(shù)默認(rèn)為0 -->
- <property name="bonecp.minConnectionsPerPartition" value="20"/>
- <!-- 設(shè)置statement緩存?zhèn)數(shù)。這個(gè)參數(shù)默認(rèn)為0 -->
- <property name="bonecp.statementsCacheSize" value="50"/>
- <!-- 設(shè)置連接助手線程個(gè)數(shù)。這個(gè)參數(shù)默認(rèn)為3 -->
- <property name="bonecp.releaseHelperThreads" value="3"/> </properties>
- </persistence-unit>
- </persistence>
復(fù)制代碼 配置數(shù)據(jù)源時(shí),我個(gè)人不喜歡配置多個(gè)數(shù)據(jù)源。以上使用的數(shù)據(jù)源是BoneCP數(shù)據(jù)源,性能比C3P0好,當(dāng)然也不是說不用C3P0了。下次我會(huì)給出C3P0的配置代碼及其Jar包
|