BoneCP (Java database connection pool ) 설정 및 사용
Bonecp는 dbcp, c3p0 를 대체하는 고성능의 Java database connection pool 구현물.
사용조건
- JDK 1.5 이상
- Google Guava library 필요
- SLF4J logging library 필요
설정 값 및 성능 자료
설정값
- partitionCount : Pool 에서 connection 을 가져올때 lock 경쟁을 최소화하기 위해 pool 을 여러 개 파티션으로 나눌수 있음. 기본값은 1이며 높은 성능이 필요할 때 3~4 정도 추천
- maxConnectionsPerPartition: 파티션별 Connection 수. 전체 Connection 은 partitionCount * maxConnectionsPerPartion 이 됨.(3개 파티션이 있고 maxConnectionsPerPartion 이 5면 전체 DB Connection 은 15가 됨)
- acquireIncrement: 파티션내 Connection 이 모두 소모되었을때 늘릴 Connection Count. 기본값은 10 이며 전체 Count가 아니라 파티션별 count임
성능 비교
WAS의 JNDI 로 사용
TOMCAT 설정
JNDI 로 사용시 WAS 구동시 BoneCP가 의존하는 library 를 찾기때문에 WAS의 lib 폴더에 의존성있는 library 를 넣어야 deploy 문제 없음
- bonecp-0.8.0.RELEASE.jar
- guava-15.0.jar
- slf4j-log4j12-1.7.5.jar
- slf4j-api-1.7.5.jar
- log4j 등 slf4j 에 bind해서 사용하는 library
- releaseHelperThreads 는 deprecated 이다.
- IdleMaxAge 대신 idleMaxAgeInMinutes 를 써야 한다.
- idleConnectionTestPeriod 대신 idleConnectionTestPeriodInMinutes 를 써야 한다.
https://github.com/wwadge/bonecp/blob/master/bonecp/src/main/resources/bonecp-default-config.xml
<Resource name="jdbc/spring-demo" auth="Container" driverClass="com.mysql.jdbc.Driver" username="springdemo" password="demo1234" idleConnectionTestPeriodInMinutes="60" idleMaxAgeInMinutes="60" maxConnectionsPerPartition="30" minConnectionsPerPartition="5" partitionCount="3" acquireIncrement="5" statementsCacheSize="100" factory="org.apache.naming.factory.BeanFactory" type="com.jolbox.bonecp.BoneCPDataSource" jdbcUrl="jdbc:mysql://localhost:3306/springdemo?autoReconnect=true&useUnicode=true&characterEncoding=utf8" />
tomcat의 경우 Context.xml 에 ResourceLink를 추가해 줘야 한다.
<Context> <ResourceLink name="jdbc/spring-demo" global="jdbc/spring-demo" type="javax.sql.DataSource"/> </Context>
Spring의 AppContext
<?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:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <jee:jndi-lookup id="jdbc/spring-demo" jndi-name="jdbc/spring-demo" expected-type="javax.sql.DataSource" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="jdbc/spring-demo" p:mapperLocations="classpath:/mycom/mypkg/mapper/*mapper.xml" p:configLocation="classpath:/mycom/mypkg/mybatis-config.xml" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="mycom.mypkg.mapper" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="jdbc/spring-demo" /> </beans>
Spring 의 appContext에서 사용
Application Context 에 다음 내용 추가
<?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:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- BoneCP configuration --> <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springdemo?autoReconnect=true&useUnicode=true&characterEncoding=utf8" /> <property name="username" value="springdemo"/> <property name="password" value="demo1234"/> <property name="idleConnectionTestPeriod" value="60"/> <property name="idleMaxAge" value="240"/> <property name="maxConnectionsPerPartition" value="30"/> <property name="minConnectionsPerPartition" value="10"/> <property name="partitionCount" value="3"/> <property name="acquireIncrement" value="5"/> <property name="statementsCacheSize" value="100"/> <property name="releaseHelperThreads" value="3"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="mainDataSource" p:mapperLocations="classpath:/mycompany/mypkg/mapper/*mapper.xml" p:configLocation="classpath:/mycompany/mypkg/mybatis-config.xml" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="mycompany.mypkg.mapper" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="mainDataSource" /> </beans>
maven pom.xml 에 추가
<dependencies> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> </dependencies> <repositories> <repository> <releases> <enabled>true</enabled> </releases> <id>bonecp-repo</id> <name>BoneCP Repository</name> <url>http://jolbox.com/bonecp/downloads/maven</url> </repository> </repositories>
log4j 설정
log4j.category.com.jolbox=DEBUG,Console
See Also
Ref