DbUtil?
843854Nov 14 2003 — edited Nov 14 2003Hi,
Does anyone know what class is needed for using DbUtil()?
I have the following code, but dbutil seems to be cauing an error when compiling:
public class CAServlet
extends JAXMServlet
implements ReqRespListener {
private static String NS_PREFIX = "";
private static String NS_URI = "http://rd-arachne.juniper.net:7013/search/CAwebservice.wsdl";
private static final String SQL_PING = "select * from users where 1=2";
private static final String SQL_ROLES = "select roles from documents where url=?";
private static final String CONNECT_MUTEX = "connect_mutex";
private static final String RECONNECT_MUTEX = "reconnect_mutex";
private boolean debug = false;
private Name id = null;
private Name href = null;
private SOAPElement curSearchElem = null;
private Hashtable idHash = null;
private Iterator SearchIDIt = null;
static private String _dbUrl;
static private String _dbDriver;
static private String _dbUser;
static private String _dbPassword;
// maintain only one instance of database Connection
private Connection conn;
//static Logger logger = Logger.getLogger(CAServlet.class);
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String strDebug = servletConfig.getInitParameter("debug");
if (strDebug != null) {
if (strDebug.equalsIgnoreCase("true") || strDebug.equals("1"))
debug = true;
}
ServletContext servletContext = servletConfig.getServletContext();
_dbDriver = servletContext.getInitParameter("DatabaseDriver");
_dbUrl = servletContext.getInitParameter("DatabaseUrl");
_dbUser = servletContext.getInitParameter("DatabaseUser");
_dbPassword = servletContext.getInitParameter("DatabasePassword");
if (_dbDriver == null) {
//logger.error("Error in web.xml: DatabaseDriver is not specified");
throw new ServletException("Error in web.xml: DatabaseDriver is not specified");
}
if (_dbUrl == null) {
//logger.error("Error in web.xml: DatabaseUrl is not specified");
throw new ServletException("Error in web.xml: DatabaseUrl is not specified");
}
if (_dbUser == null) {
//logger.error("Error in web.xml: DatabaseUser is not specified");
throw new ServletException("Error in web.xml: DatabaseUser is not specified");
}
if (_dbPassword == null) {
//logger.error("Error in web.xml: DatabasePassword is not specified");
throw new ServletException("Error in web.xml: DatabasePassword is not specified");
}
idHash = new Hashtable ();
try {
DbUtil.loadDbDriver(_dbDriver);
dbConnect();
dbPing();
//logger.info("CAServlet:init:: Connection to database succeeded.");
} catch (Exception e) {
//logger.error("Database connection failed. Error: " + e.getMessage() + ". Servlet exiting.");
//debug = true; //Rajesh
}
}
// connect to the database
private void loadDbDriver() throws Exception
{
// try to load the specified JDBC driver class
try {
Class.forName(_dbDriver).newInstance();
} catch (InstantiationException ie) {
throw new Exception("JDBC driver class instantiation exception: " +
ie.getMessage());
} catch (ClassNotFoundException cnfe) {
throw new Exception("JDBC driver class not found: " +
cnfe.getMessage());
} catch (IllegalAccessException iae) {
throw new Exception("JDBC driver class access exception: " +
iae.getMessage());
}
}
private void dbConnect() throws Exception
{
synchronized (this.CONNECT_MUTEX) {
boolean do_reconnect = false;
if (conn == null) {
do_reconnect = true;
} else {
try {
if (conn.isClosed()) {
do_reconnect = true;
}
} catch (SQLException sqle2) {
do_reconnect = true;
}
}
if (do_reconnect) {
// try to connect to the database
conn = DriverManager.getConnection(_dbUrl); //, dbUser, dbPassword);
}
}
}
private void dbReconnect() throws Exception
{
synchronized (this.RECONNECT_MUTEX) {
// ping one last time now inside a synchronized block
// in case another thread got here first
// and reestablished the connection
DbUtil dbu = new DbUtil();
if (dbu.dbPing(conn, SQL_PING)) {return;}
// close and open a connection
if (conn != null) {
try {conn.close();}
catch (Exception e) {}
}
conn = DriverManager.getConnection(_dbUrl); //, dbUser, dbPassword);
}
}
private void dbPing() throws Exception
{
DbUtil dbu = new DbUtil();
boolean ret = dbu.dbPing(conn, SQL_PING);
}
//...
//...
//...
}
Thanks,
Chris