Global variable in servlet & DBPooling questions
843841Dec 27 2005 — edited Dec 28 2005Hello guys,
I used to develop PHP/ASP, and am new to servlet. I have been searching around for a solution ...
With Php, we can get the reference of a global variable in any classes->functions...
How do I do this with servlet ?
And second..I have developed the DB class as below... I set the datasource to be static, so it initializes only once. Is it a good idea? How would you like to improve this class? any comments?
package shop.database;
import javax.sql.DataSource;
import java.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import shop.admin.exception.GeneralException;
public class DdManager {
static protected Logger logger = Logger.getLogger(DdManager.class);
private String userName = "root";
private String password = "";
private String hostName = "jdbc:mysql://localhost:3306/shop";
private String database="shop";
static private DataSource ds; // set this to be static so all threads share the same job in JVM
private Statement stmt;
private Connection conn;
private ResultSet rs;
private CallableStatement cs;
public DdManager() {}
/*
* setup the data source and return it
*/
public static DataSource getDataSource(
String sDrvName,
String sUserName,
String sPwd,
String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName( sDrvName );
ds.setUsername( sUserName );
ds.setPassword( sPwd );
ds.setUrl( connectURI );
ds.setMaxActive( 15 );
ds.setMaxIdle( 10 );
ds.setMaxWait( 10000 ); // 10 seconds
return ds;
}
/*
* static init of the class
* this class is will be called only once to initialize the DataSource
*/
static {
try {
Class.forName( "com.mysql.jdbc.Driver" );
ds = getDataSource( "com.mysql.jdbc.Driver",
"root",
"",
"jdbc:mysql://localhost:3306/shop" );
if (ds == null) {
String msg = "Connection Pool error";
logger.error(msg);
throw new GeneralException(msg);
}
logger.info("DataSource has been initialized");
} catch(Exception exception) {
logger.error(exception.toString());
try {
throw new GeneralException(exception.toString());
} catch (GeneralException e) {
logger.error(e.toString());
}
}
}
/*
* get the connection from the pool (DataSource)
*/
public void openConnection() throws GeneralException {
try {
BasicDataSource bds = (BasicDataSource) ds;
logger.info("NumActive: " + bds.getNumActive() + ", " + "NumIdle: " + bds.getNumIdle());
conn = ds.getConnection();
logger.info("Connection of " + database + " has been established");
} catch(Exception exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
/*
* close the connection will actually return the connection to the pool (Must)
*/
public void closeConnection() throws GeneralException {
initResource();
try {
if (conn != null){
conn.close();
logger.info("Connection of " + database + " has been closed");
}
} catch(SQLException exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
/*
* prepare the calling stmt
*/
public void prepareProcedure(String callStatement) throws GeneralException {
initResource();
try {
cs = conn.prepareCall(callStatement);
} catch(SQLException exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
/*
* set the pass-in parameter for "String"
*/
public void setParameter(int position, String parameter) throws GeneralException {
try {
cs.setString(position, parameter);
} catch(Exception exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
/*
* set the pass-in parameter for "Integer"
*/
public void setParameter(int position, int parameter) throws GeneralException {
try {
cs.setInt(position, parameter);
} catch(Exception exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
/*
* execute the procedure and return the resultset
*/
public ResultSet execProcedure() throws GeneralException {
try {
rs = cs.executeQuery();
} catch(SQLException exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
return rs;
}
/*
* close the statment and resultset
*/
private void initResource() throws GeneralException {
try {
if(rs != null) {
rs.close();
}
if(stmt!= null) {
stmt.close();
}
logger.info("Statement & Resultset have been free");
} catch(Exception exception) {
logger.error(exception.toString());
throw new GeneralException(exception.toString());
}
}
}
Thanks mates!
myy