1 package jodd.db.pool; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.naming.Context; 7 import javax.naming.InitialContext; 8 import javax.naming.NamingException; 9 import javax.sql.DataSource; 10 11 14 public class ContextConnectionPool implements ConnectionPool { 15 16 18 private String envContextName; 19 public void setEnvContextName(String s) { 20 envContextName = s; 21 } 22 23 private String dataSourceName; 24 public void setDataSourceName(String s) { 25 dataSourceName = s; 26 } 27 28 30 private DataSource ds; 31 32 public void init() throws SQLException { 33 try { 34 Context initContext = new InitialContext(); 35 Context envContext = (Context) initContext.lookup(envContextName); 36 ds = (DataSource) envContext.lookup(dataSourceName); 37 } catch (NamingException ne) { 38 throw new SQLException("Context can not be found: " + envContextName + ", " + dataSourceName); 39 } 40 } 41 42 public void close() { 43 } 44 45 46 48 public Connection getConnection() throws SQLException { 49 return ds.getConnection(); 50 } 51 52 57 public void freeConnection(Connection conn) { 58 try { 59 if (conn != null) { 60 conn.close(); 61 } 62 } catch (SQLException e) { 63 } 64 } 65 66 67 } 68 69 | Popular Tags |