난 내 자신의 연결 풀 클래스struts-config.xml에 내 자신의 연결 풀을 초기화하는 방법은 무엇입니까?
public class ConnectionPool {
private static final Logger log = Logger.getLogger(ConnectionPool.class);
public final static String PROPERTIES_FILENAME = "config";
public static final int DEFAULT_POOL_SIZE = 10;
//single instance
private static ConnectionPool instatance;
//queue of free connections
private BlockingQueue<Connection> connectionQueue;
public ConnectionPool(String driver, String url, String user,
String password, int poolSize)
throws ClassNotFoundException, DAOException{
try{
Class.forName(driver);
connectionQueue = new ArrayBlockingQueue<Connection>(poolSize);
for(int i = 0; i < poolSize ;i++){
Connection connection = DriverManager.getConnection(url, user, password);
connectionQueue.offer(connection);
}
}
catch (SQLException e) {
log.error(e);
throw new DAOException(e.getMessage());
}
}
public static void init() throws DAOException{
try {
if(instatance == null){
String driver = ConfigurationManager.
getInstance().getProperty("DATABASE_DRIVER_NAME");
String url = ConfigurationManager.
getInstance().getProperty("DATABASE_URL");
String user = ConfigurationManager.
getInstance().getProperty("DATABASE_USER");
String password = ConfigurationManager.
getInstance().getProperty("DATABASE_PASSWORD");
String poolSizeStr = ConfigurationManager.
getInstance().getProperty("DATABASE_POOLSIZE");
int poolSize = (poolSizeStr != null) ?
Integer.parseInt(poolSizeStr) : DEFAULT_POOL_SIZE;
log.info("Trying to create pool of connections...");
instatance = new ConnectionPool(driver,url,user,password,poolSize);
log.info("Connection pool initialized");
}
}catch (ClassNotFoundException e) {
log.error(e);
} catch (SQLException e) {
log.error(e);
throw new DAOException(e.getMessage());
}
}
public static void dispose() throws DAOException {
try {
if(instatance != null){
instatance.clearConnectionQueue();
instatance = null;
log.info("Connection queue is disposed");
}
} catch (DAOException e) {
log.info(e.getMessage());
throw new DAOException(e.getMessage());
}
}
public static ConnectionPool getInstance(){
return instatance;
}
public Connection takeConnection() {
Connection connection = null;
try{
connection = connectionQueue.take();
}catch (InterruptedException e) {
log.info("Free connection waiting interrupted.Returned null connection");
log.error(e);
}
return connection;
}
public void releaseConnection(Connection connection) throws DAOException {
try {
if(!connection.isClosed()){
if(!connectionQueue.offer(connection)){
log.info("Connections is not added.");
}
}
else{
log.info("Trying to release closed connection.");
}
} catch (SQLException e) {
log.info("SQLException at connection isClosed(). Connection is not added");
throw new DAOException(e.getMessage());
}
}
private void clearConnectionQueue() throws DAOException{
try {
Connection connection;
while((connection = connectionQueue.poll()) != null){
if(!connection.getAutoCommit()){
connection.commit();
connection.close();
}
}
} catch (SQLException e) {
log.info(e.getMessage());
throw new DAOException(e.getMessage());
}
}
이 있고이 스트럿 서블릿의 라이프 사이클의 시작에서 초기화해야합니다. 그것은 일반 서블릿 있다면 나는 init()
방법은 생각하지 사용했던, 나는 그것을 할 거라고 :
public class LinkAction extends DispatchAction {
private static final String PARAM_NAME_LANGUAGE = "language";
/**
* This is the Struts action method called on
* http://.../actionPath?method=myAction1,
* where "method" is the value specified in <action> element :
* (<action parameter="method" .../>)
*/
private static ConnectionPool connectionPool =
ConnectionPool.getInstance();
public void init(){
try {
if(connectionPool == null){
ConnectionPool.init();
connectionPool = ConnectionPool.getInstance();
}
} catch (DAOException e) {
log.error(e);
}
}
public ActionForward newsList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("newsList");
}
public ActionForward addNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("addNews");
}
public ActionForward changeLocale(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String localeValue = request.getParameter("localeValue");
request.getSession().setAttribute(PARAM_NAME_LANGUAGE, localeValue);
return mapping.findForward("newsList");
}
}
그러나 이것은 Struts 액션 작동하지 않습니다, 그래서 그것이 struts-config.xml
또는에서 수행 될 수 있다는 결정 web.xml
. 그러나 어떻게?