[Appsunit-develop] [cvscommit] project/src/net/sourceforge/appsunit/environment DbSession.java, 1.
Status: Beta
Brought to you by:
jancumps
|
From: Jan C. <jan...@us...> - 2006-09-18 09:23:47
|
Update of /cvsroot/appsunit/project/src/net/sourceforge/appsunit/environment In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv5820/src/net/sourceforge/appsunit/environment Modified Files: DbSession.java Log Message: Preparing for database connection setup. Index: DbSession.java =================================================================== RCS file: /cvsroot/appsunit/project/src/net/sourceforge/appsunit/environment/DbSession.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DbSession.java 11 Sep 2006 20:32:52 -0000 1.1 --- DbSession.java 18 Sep 2006 09:23:42 -0000 1.2 *************** *** 11,14 **** --- 11,30 ---- package net.sourceforge.appsunit.environment; + import java.sql.Connection; + import java.sql.SQLException; + import java.sql.Statement; + + import javax.sql.DataSource; + + import org.apache.commons.dbcp.ConnectionFactory; + import org.apache.commons.dbcp.DriverManagerConnectionFactory; + + import org.apache.commons.pool.ObjectPool; + + import org.apache.commons.pool.impl.GenericObjectPool; + + import org.apache.commons.dbcp.PoolableConnectionFactory; + import org.apache.commons.dbcp.PoolingDataSource; + /** * *************** *** 18,25 **** --- 34,136 ---- /** + * Stores the datasource that will serve database connections. + */ + private static DataSource dataSource; + + /** * Creates a new instance of DbSession */ public DbSession() { } + + /** + * Getter for property connection. + * @return Value of property connection. + * @throws java.lang.Exception Exceptions are not caught. They will be passed to the caller. + * @TODO: parameterize nls settings + */ + public static Connection getConnection() throws Exception { + Connection connection = null; + + + try { + if (dataSource != null ) + connection = dataSource.getConnection(); + + String driver = null; + driver = System.getProperty("db.driver","oracle.jdbc.OracleDriver"); + Class.forName(driver); + // Attempt to connect to a driver. Each one + // of the registered drivers will be loaded until + // one is found that can process this URL + + String uri = System.getProperty("db.uri","jdbc:oracle:thin:user/password@server:port:sid"); + dataSource = setupDataSource(uri); + connection = dataSource.getConnection(); + + String q = null; + Statement s = connection.createStatement(); + + q = "alter session set nls_language = 'American'"; + s.execute(q); + q = "alter session set nls_territory = 'America'"; + s.execute(q); + s.close(); + + + } catch(SQLException e) { + throw e; + + } catch(ClassNotFoundException e) { + throw e; + + } catch (Exception e) { + throw e; + + } + + return connection; + } + + /** + * Configures the datasource. + * @param connectURI URI connection string to the database. + * @throws java.lang.Exception Exceptions are not caught. They will be passed to the caller. + * @return The configured datasource. + */ + private static DataSource setupDataSource(String connectURI) throws Exception { + // + // First, we'll need a ObjectPool that serves as the + // actual pool of connections. + // + // We'll use a GenericObjectPool instance, although + // any ObjectPool implementation will suffice. + // + ObjectPool connectionPool = new GenericObjectPool(null, 1); + + // + // Next, we'll create a ConnectionFactory that the + // pool will use to create Connections. + // We'll use the DriverManagerConnectionFactory, + // using the connect string passed in the command line + // arguments. + // + ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null); + + // + // Now we'll create the PoolableConnectionFactory, which wraps + // the "real" Connections created by the ConnectionFactory with + // the classes that implement the pooling functionality. + // + PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true); + + // + // Finally, we create the PoolingDriver itself, + // passing in the object pool we created. + // + PoolingDataSource dataSource = new PoolingDataSource(connectionPool); + + return dataSource; + } } |