avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/util/test DataSourceFactoryRunner.java
Date Thu, 27 Jun 2002 09:47:34 GMT
leosutic    2002/06/27 02:47:34

  Added:       microcontainer/src/java/org/apache/excalibur/microcontainer/util
                        DataSourceFactory.java
               microcontainer/src/test/org/apache/excalibur/microcontainer/util/test
                        DataSourceFactoryRunner.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/util/DataSourceFactory.java
  
  Index: DataSourceFactory.java
  ===================================================================
  package org.apache.excalibur.microcontainer.util;
  
  import org.apache.avalon.excalibur.datasource.DataSourceComponent;
  import org.apache.avalon.excalibur.datasource.JdbcDataSource;
  
  import org.apache.avalon.framework.configuration.DefaultConfiguration;
  
  import org.apache.excalibur.microcontainer.MicroContainer;
  import org.apache.excalibur.microcontainer.MicroContainerException;
  
  public class DataSourceFactory
  {
      private static final DefaultConfiguration configurationWithValue( String name, String
value ) 
      {
          DefaultConfiguration child = new DefaultConfiguration( name, "" );
          child.setValue( value );
          return child;
      }
      
      public static DataSourceComponent getDataSource( 
          String driver, String dbUrl, String user, 
          String password, String keepAliveCommand, 
          int poolMin, int poolMax, long timeout, 
          boolean autoCommit, boolean useOracle, 
          String connectionClass ) throws MicroContainerException 
      {
          DefaultConfiguration config = new DefaultConfiguration( "", "" );
          config.addChild( configurationWithValue( "driver", driver ) );
          config.addChild( configurationWithValue( "dburl", dbUrl ) );
          config.addChild( configurationWithValue( "user", user ) );
          config.addChild( configurationWithValue( "password", password ) );
          config.addChild( configurationWithValue( "auto-commit", String.valueOf( autoCommit
) ) );
          
          DefaultConfiguration controller = new DefaultConfiguration( "pool-controller", ""
);
          controller.setAttribute( "min", String.valueOf( poolMin ) );
          controller.setAttribute( "max", String.valueOf( poolMax ) );
          controller.setAttribute( "timeout", String.valueOf( timeout ) );
          controller.setAttribute( "oradb", String.valueOf( useOracle ) );
          
          if( connectionClass != null )
          {
              controller.setAttribute( "connection-class", connectionClass );
          }
          
          DefaultConfiguration keepAlive = new DefaultConfiguration( "keep-alive", "" );
          if( keepAliveCommand != null )
          {
              keepAlive.setValue( keepAliveCommand );
          }
          else
          {
              keepAlive.setAttribute( "disable", "true" );
          }
          
          controller.addChild( keepAlive );
          config.addChild( controller );
                  
          return (DataSourceComponent) new MicroContainer( JdbcDataSource.class ).configuration(
config ).create();
      }
      
      
  }
  
  
  1.1                  jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/util/test/DataSourceFactoryRunner.java
  
  Index: DataSourceFactoryRunner.java
  ===================================================================
  package org.apache.excalibur.microcontainer.util.test;
  
  import java.sql.*;
  
  import org.apache.avalon.excalibur.datasource.DataSourceComponent;
  import org.apache.avalon.excalibur.datasource.JdbcDataSource;
  
  import org.apache.excalibur.microcontainer.MicroContainer;
  import org.apache.excalibur.microcontainer.util.DataSourceFactory;
  
  public class DataSourceFactoryRunner
  {
      public static void main( String[] args ) throws Exception 
      {
          DataSourceComponent ds = DataSourceFactory.getDataSource( 
              "org.gjt.mm.mysql.Driver", 
              "jdbc:mysql://localhost/",
              "sa",
              "",
              null,
              1,
              3,
              -1,
              true,
              false,
              null);
          
          Connection conn = null;
          Statement stmt = null;
          ResultSet rs = null;
          try 
          {
              conn = ds.getConnection();
              stmt = conn.createStatement();
              rs = stmt.executeQuery( "SHOW DATABASES;" );
              
              while (rs.next()) 
              {
                  System.out.println( rs.getObject(1) );
              }
          }
          finally
          {
              if( rs != null )
              {
                  rs.close();
              }
              
              if( stmt != null )
              {
                  stmt.close();
              }
              
              if( conn != null )
              {
                  conn.close();
              }
              
              MicroContainer.release(ds);
          }
      }
  }
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message