mybatis 3 settings
[MyBatis singleton class]
package meeton.web.util.mybatis;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatis {
private static final MyBatis self = new MyBatis();
public static MyBatis getInstance() {
return self;
}
private SqlSessionFactory factory;
private MyBatis() {
reload();
}
public SqlSessionFactory getFactory() {
return factory;
}
public void reload() {
try {
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.xml"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
[mybatis.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="config.properties"/>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="poolMaximumActiveConnections" value="20"/>
<property name="poolMaximumIdleConnections" value="20"/>
<property name="poolMaximumCheckoutTime" value="20000"/>
<property name="poolPingEnabled" value="true"/>
<property name="poolPingQuery" value="select 1"/>
<property name="poolPingConnectionsNotUsedFor" value="43200"/>
<property name="poolTimeToWait" value="30000"/>
<property name="driver.encoding" value="UTF-8"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="root/sub1/sub2/sample.xml"/>
</mappers>
</configuration>
[sample.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.mysql">
<select id="selectAllUsers" resultType="Map">
select * from user
</select>
<select id="selectUser" parameterType="String" resultType="Map">
select * from user where user = #{user}
</select>
</mapper>
출처 - http://yousik.blogspot.com/2012/04/mybatis-singleton-class-package-meeton.html
==================================================================================
여기서 눈여겨 볼 사항이 <property name="poolPingQuery" value="select 1"/> 이다.
기존에 validationQuery 대신 poolPingQuery로 사용 하고 있다.
다음은 각 pool 과 관련된 속성 값 설명이다.
<property name="poolMaximumActiveConnections" value="20"/>
: 동시 활성화 할 커넥션 수
<property name="poolMaximumIdleConnections" value="20"/>
: 유휴상태의 커넥션 수
<property name="poolMaximumCheckoutTime" value="20000"/>
: 커넥션 요청 후 획득까지 기다리는 시간
<property name="poolPingEnabled" value="true"/>
: 커넥션 ping 테스트
<property name="poolPingQuery" value="select 1"/>
: 커넥션이 살아 있는지 확인할 쿼리
<property name="poolPingConnectionsNotUsedFor" value="43200"/>
: 커넥션이 얼마 동안 유휴상태면 닫히는지 판단하는 시간
<property name="poolTimeToWait" value="30000"/>
: 사용 불가능한 커넥션 기다리는 시간
<property name="poolPingConnectionsOlderThan" value="43200"/>
: 어떤 커넥션이 닫힐 상태인지 판단하는 기준시간
'Framework & Platform > mybatis' 카테고리의 다른 글
insert 구문에서 session.commit() 적용 (0) | 2012.05.01 |
---|---|
useGenerateKeys 사용하기 예제 (0) | 2012.05.01 |
mybatis password 암호화/복호화 (0) | 2012.04.28 |
Mybatis selectList 예제 (0) | 2012.03.28 |
mybatis 설치 예제 (0) | 2012.03.18 |