/* * ConnectionPool.java * * Created on 3 février 2005, 14:25 */ package fr.cerene.geosiara.gprs.receiver; import javax.sql.DataSource ; import java.sql.*; import java.io.*; import java.util.*; import org.apache.commons.dbcp.*; import org.apache.commons.pool.impl.*; import org.apache.commons.pool.*; import org.apache.log4j.Logger; /** * * @author steff */ public class ConnectionPool { private static DataSource datasource = setupDataSource() ; private final static ConnectionPool connectionPool = new ConnectionPool(); private static Logger logger = Logger.getLogger(ConnectionPool.class.getName()); /** Creates a new instance of ConnectionPool */ private ConnectionPool() { } public static ConnectionPool getRef() { return connectionPool ; } private static DataSource setupDataSource() { try { Properties conf = new Properties(); FileInputStream fis = new FileInputStream("conf/conf.properties"); conf.load(fis); String dbd = conf.getProperty("jdbc_driver","org.postgresql.Driver"); String dbc = conf.getProperty("db_connection_string","jdbc:postgresql://200.200.200.34:5432/geosiara2"); String dbu = conf.getProperty("dbuser","apache"); String dbp = conf.getProperty("dbpass","apache"); BasicDataSource ds = new BasicDataSource(); ds.setUrl(dbc); ds.setDriverClassName(dbd); ds.setUsername(dbu); ds.setPassword(dbp); ds.setMaxActive(10); ds.setMaxIdle(1); ds.setPoolPreparedStatements(true); ObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds); PoolableObjectFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,false); PoolingDataSource dataSource = new PoolingDataSource(connectionPool); return dataSource; }catch(IOException e) { logger.fatal(e.toString()); System.exit(1); return null; } } public static void shutdownDataSource() throws SQLException { BasicDataSource bds = (BasicDataSource) datasource; bds.close(); } public Connection getConnection() { Connection conn = null; try { conn = datasource.getConnection(); }catch(SQLException e){} return conn; } public static void main(String[] args) { try { Connection conn = ConnectionPool.getRef().getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM gps_frame"); while(rs.next()) { System.out.println(rs.getString("sbox_id")); } rs.close(); stmt.close(); conn.close(); }catch(SQLException e){System.out.println(e);} } }