Web/JSP
JSP) 커넥션 풀
surhommejk
2018. 4. 23. 10:03
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- maxActive : 최대 동시 대여 가능 커넥션수
maxIdle : 최소 보유 커넥션수
maxWait : 최대 대기 시간 -> 넘겼는데도 빈 커넥션이 없으면 Exception (1=5초, 180=15분)
removeAbandoned : (true) 대여시간 초과했는데 반납 안된 커넥션 처리
removeAbandonedTimeout : 대여시간 설정 ( 1=5초 60=5분 )
logAbandoned : (true) 삭제시에 로그를 남길 것인가?
validationQuery : 커넥션이 살아 있는지 체크 하는 쿼리로
오라클의 경우에는 "select 1 from dual", MySQL의 경우에는 "select 1"
testWhileIdle : (true) 커넥션에 아무런 데이터 송수신이 없을 경우
테스트를 할지 여부를 결정합니다.
timeBetweenEvictionRunsMillis : 커넥션이 쉬고 있을 때 커넥션 체크 쿼리를 실행하는
시간 간격을 설정합니다. 밀리 세컨드로 설정하며,
저보통 60000(1분)을 줍니다. -->
<Resource maxWait="-1"
maxIdle="20"
maxActive="100"
password="1004"
username="bituser"
url="jdbc:oracle:thin:@localhost:1521:XE"
driverClassName="oracle.jdbc.OracleDriver"
type="javax.sql.DataSource"
auth="Container"
name="jdbc/oracle" />
</Context>
public class boarddao {
static DataSource ds;
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
static {
InitialContext ctx;
try {
ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("/jdbc/oracle");
} catch (NamingException e) {
System.out.println("lookup Fail : " + e.getMessage());
}
}
// 이제 각 함수에서 conn = ds.getConnection(); 사용하여 connection 만들고 사용
public int writeok(board boardata) throws Exception {
try {
conn = ds.getConnection(); // <- 이렇게 DataSource에서 꺼내쓴다
String sql = "insert into jspboard(idx,writer,pwd,subject,content,
email,homepage,writedate,readnum,filename,filesize,refer) values("
+ " jspboard_idx.nextval,?,?,?,?,?,?,sysdate,0,?,0,?)";
pstmt = conn.prepareStatement(sql);
// parameter 설정하기
pstmt.setString(1, boardata.getWriter());
pstmt.setString(2, boardata.getPwd());
pstmt.setString(3, boardata.getSubject());
pstmt.setString(4, boardata.getContent());
pstmt.setString(5, boardata.getEmail());
pstmt.setString(6, boardata.getHomepage());
pstmt.setString(7, boardata.getFilename());
int refer_max = getMaxRefer(conn);
int refer = refer_max + 1;
pstmt.setInt(8, refer);
int row = pstmt.executeUpdate();
return row;
}
finally {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}
}
}
cf) 반환을 까먹고 안하면 비동기와 같이 계속된 요청을 하게 될 시에 프로그램이 터진다.
빌려쓰기만 하고 반환을 안했기 때문이다