ContextListener를 사용하여 연결 풀을 만들고이 프로젝트를 tomcat.my config 부분에 호스팅합니다.서버가 다운 될 때마다 연결이 닫힙니다. 시간 초과 10000000을 추가했습니다.
tomcat confg context.xml 아래에 리소스를 정의했습니다.
<Resource name="jdbc/TEST_DS"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@server:1521/db"
username="uname"
password="pwd"
maxPoolSize="50"
removeAbandoned="true"
removeAbandonedTimeout="1000"
logAbandoned="true"
/>
이제이 리소스를 아래의 ContextListener에서 사용하십시오.
public class ApplicationUtil {
private static ServletContext context;
/* Called by Listener */
public static void setServletContext(ServletContext context){
ApplicationUtil.context = context;
}
/* Use this method to access context from any location */
public static ServletContext getServletContext(){
return ApplicationUtil.context;
}
}
public class DBAccess {
ServletContext context = null;
public DBAccess(ServletContext cnt) {
context = cnt;
}
public Connection getOracleConnection() throws SQLException, ClassNotFoundException {
return ConnectionListener.getInstance(context).getOracleConnection();
}
public List getLanguageList() {
Connection cn = getOracleConnection();
...
}
}
이 모두가 내가 연결 풀을 만들기 위해 만든 것입니다
public class ConnectionListener implements ServletContextListener {
private DataSource dataSourceOracle = null;
private Connection connectionOracle = null;
private static final String ATTRIBUTE_NAME = "config";
public void contextDestroyed(ServletContextEvent sce) {
try {
if(connectionOracle!=null && !connectionOracle.isClosed()){
this.connectionOracle.close();
this.connectionOracle = null;
}
ApplicationUtil.setServletContext(sce.getServletContext());
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
try {
String oracleDsName = servletContext.getInitParameter("oracle.ds.name");
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataSourceOracle = (DataSource) envContext.lookup (oracleDsName);
connectionOracle = dataSourceOracle.getConnection();
System.out.println("testing Oracle connection >> "+connectionOracle);
ApplicationUtil.setServletContext(event.getServletContext());
} catch (SQLException e) {
e.printStackTrace();
}
catch (NamingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
servletContext.setAttribute(ATTRIBUTE_NAME, this);
}
public Connection getOracleConnection() throws SQLException, ClassNotFoundException {
return this.connectionOracle;
}
public static ConnectionListener getInstance(ServletContext servletContext) {
return (ConnectionListener) servletContext.getAttribute(ATTRIBUTE_NAME);
}
}
이제이 연결을 사용하여 메서드를 호출합니다. 이제 문제는 서버가 다운 연결이 닫힐 때입니다. 그리고 연결 풀을 다시 만들 때마다 바람둥이를 다시 시작해야합니다.
해결할 영구 해결책이 있습니까?
의견을 보내 주시면 감사하겠습니다.
감사합니다.
나는 연결 풀링을 사용하는 끔찍한 방법을 느낍니다. 어쨌든 DB를 다시 시작한 후에도 연결 풀이 생존하지 않습니다. – CuriousMind
tomcat-jdbc, boncep 등 타사 라이브러리에서 제공하는 기성 연결 풀을 사용하지 않는 이유는 무엇입니까? –