1 25 package org.snipsnap.util; 26 27 import org.apache.commons.dbcp.ConnectionFactory; 28 import org.apache.commons.dbcp.DriverManagerConnectionFactory; 29 import org.apache.commons.dbcp.PoolableConnectionFactory; 30 import org.apache.commons.dbcp.PoolingDataSource; 31 import org.apache.commons.pool.ObjectPool; 32 import org.apache.commons.pool.impl.GenericObjectPool; 33 import org.radeox.util.logging.Logger; 34 import org.snipsnap.app.Application; 35 import org.snipsnap.config.Configuration; 36 37 import javax.sql.DataSource ; 38 import java.sql.Connection ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.Statement ; 42 43 49 public class ConnectionManager { 50 private static ConnectionManager instance; 51 52 public static ConnectionManager getInstance() { 53 if (null == instance) { 54 instance = new ConnectionManager(); 55 } 56 return instance; 57 } 58 59 public static synchronized void removeInstance() { 60 if (null != instance) { 61 instance = null; 62 } 63 } 64 65 private DataSource dataSource = null; 66 67 private ConnectionManager() { 68 } 69 70 private void update(Configuration config) { 71 if (null == dataSource) { 72 try { 73 System.err.println("ConnectionManager: Registering JDBC driver: " + config.getJdbcDriver()); 74 Class.forName(config.getJdbcDriver()); 75 } catch (Exception e) { 76 Logger.fatal("unable to register JDBC driver: " + config.getJdbcDriver(), e); 77 } 78 79 String jdbcUrl = config.getJdbcUrl(); 80 String jdbcPassword = config.getJdbcPassword(); 86 ObjectPool connectionPool = new GenericObjectPool(null); 92 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, config.getJdbcUser(), jdbcPassword); 93 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); 94 dataSource = new PoolingDataSource(connectionPool); 95 } 96 } 97 98 private Connection connection() { 99 update(Application.get().getConfiguration()); 100 101 try { 102 return dataSource.getConnection(); 103 } catch (Exception e) { 104 Logger.fatal("unable to get connection: ", e); 105 return null; 106 } 107 } 108 109 private DataSource dataSource() { 110 update(Application.get().getConfiguration()); 111 112 return dataSource; 113 } 114 115 public static DataSource getDataSource() { 116 return getInstance().dataSource(); 117 } 118 119 public static Connection getConnection() { 120 return getInstance().connection(); 121 } 122 123 public static void close(Connection conn) { 124 if (null != conn) { 125 try { 126 conn.close(); 127 } catch (SQLException e) { 128 } 130 } 131 } 132 133 public static void close(Statement statement) { 134 if (null != statement) { 135 try { 136 statement.close(); 137 } catch (SQLException e) { 138 } 140 } 141 } 142 143 public static void close(ResultSet result) { 144 if (null != result) { 145 try { 146 result.close(); 147 } catch (SQLException e) { 148 } 150 } 151 } 152 } 153 | Popular Tags |