Thursday, February 18, 2010

Rough Java DBCP Connection Pool Context Example Setup

Setting up initial context for DBCP connection pool.

public static void test() {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
System.setProperty(Context.PROVIDER_URL, "file:///Users/branflake2267/tmp");
InitialContext ic = null;
try {
ic = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}

// Construct BasicDataSource reference
Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "com.mysql.jdbc.Driver"));
ref.add(new StringRefAddr("type", "javax.sql.DataSource"));
ref.add(new StringRefAddr("url", "jdbc:mysql://ark/system?autoReconnect=true"));
ref.add(new StringRefAddr("username", "Web"));
ref.add(new StringRefAddr("password", "pass*7"));
ref.add(new StringRefAddr("removeAbandoned", "true"));
ref.add(new StringRefAddr("removeAbandonedTimeout", "90"));
ref.add(new StringRefAddr("logAbandoned", "true"));
ref.add(new StringRefAddr("maxActive", "1000"));
ref.add(new StringRefAddr("maxIdle", "30"));
ref.add(new StringRefAddr("maxWait", "900"));
try {
ic.rebind("jdbc/basic", ref);
} catch (NamingException e) {

e.printStackTrace();
}

InitialContext ic2 = null;
try {
ic2 = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
DataSource ds = null;
try {
ds = (DataSource) ic2.lookup("jdbc/basic");
} catch (NamingException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

No comments:

Trying out the Dart Analysis Server

I wanted to see how the Dart Analysis Server was put together and worked. I started looking to see how I could wire it up and try out the co...