You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Yun Sang Jung <na...@channeli.net> on 2000/08/18 08:55:54 UTC
How to use DBConnectionPool implemented by Singleton Pattern in struts
Hi.. all
I hope to use DBConnectionPool in struts.
So I made DBActionBase class (extends ActionBase) to use DBConnectionPool with struts
and I have called getDBConnection and freeDBConnection in perform method.
DBConnectionManager is implemented by using Singleton Pattern.
My question is "Is my below code for DBConnectionPool correct?"
Is there no chance for DBConnection to be gced?
If yes, how can I avoid gc?
====================== code begin ======================
import java.sql.*;
import org.apache.struts.action.*;
abstract public class DBActionBase extends ActionBase {
protected String dbname = "idb";
DBConnectionManager connMgr = null;
protected Connection getDBConnection() throws SQLException {
connMgr = DBConnectionManager.getInstance();
if (connMgr == null)
throw new SQLException ("Cannot access DBPoolManager");
else
return connMgr.getConnection (dbname);
}
protected void freeDBConnection (Connection conn) {
connMgr.freeConnection (dbname, conn);
}
}
====================== code end ======================
Regards,
Jung
Re: How to use DBConnectionPool implemented by Singleton Pattern in struts
Posted by Pierre Metras <ge...@sympatico.ca>.
Hi Jung,
I don't know exactly how the class instances are shared between all the servlets in an application (nor the attributes. I'll have to investigate servlets documentation). What I have done and it seems to work is follow struts documentation:
1) I wrote a Startup servlet that creates a single instance of ConnectionManager, and then keep it with
setAttribute("com.acme.idb.pool", connMgr);
2) From the ActionBase, in the perform call to obtain a reference to the servlet, retrieve the pool with
connMgr = (ConnectionMgr) servlet.getAttribute("com.acme.idb.pool");
I hope the server is responsible for synchronization on attributes access. I only have to write synchronized method to obtain and release DB connections.
Regards
Pierre Metras
----- Original Message -----
From: Yun Sang Jung
To: struts-user@jakarta.apache.org
Sent: Friday, August 18, 2000 2:55 AM
Subject: How to use DBConnectionPool implemented by Singleton Pattern in struts
Hi.. all
I hope to use DBConnectionPool in struts.
So I made DBActionBase class (extends ActionBase) to use DBConnectionPool with struts
and I have called getDBConnection and freeDBConnection in perform method.
DBConnectionManager is implemented by using Singleton Pattern.
My question is "Is my below code for DBConnectionPool correct?"
Is there no chance for DBConnection to be gced?
If yes, how can I avoid gc?
====================== code begin ======================
import java.sql.*;
import org.apache.struts.action.*;
abstract public class DBActionBase extends ActionBase {
protected String dbname = "idb";
DBConnectionManager connMgr = null;
protected Connection getDBConnection() throws SQLException {
connMgr = DBConnectionManager.getInstance();
if (connMgr == null)
throw new SQLException ("Cannot access DBPoolManager");
else
return connMgr.getConnection (dbname);
}
protected void freeDBConnection (Connection conn) {
connMgr.freeConnection (dbname, conn);
}
}
====================== code end ======================
Regards,
Jung