星期五, 6月 01, 2012

[Java] DBCP Sample

簡單的DBCP範例,主要記錄Reference物件的用法
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();
}
}

http://codintips.blogspot.tw/2010/02/rough-java-dbcp-connection-pool-context.html

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails