001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.jdbc;
007
008import java.io.*;
009import java.util.*;
010import java.util.logging.*;
011import java.sql.*;
012
013import fc.io.*;
014import fc.util.*;
015
016/** 
017A simple connection manager that returns a new connection to the
018database every time.
019
020@author hursh jain
021*/
022public class SimpleConnectionMgr extends ConnectionMgr
023{
024
025/**
026{@see ConnectionMgr} 
027**/
028public SimpleConnectionMgr(
029    String jdbc_url, String jdbc_driver, 
030    String jdbc_user, String jdbc_password, 
031    String jdbc_catalog)  
032throws Exception
033  {
034  super(jdbc_url, jdbc_driver, jdbc_user, jdbc_password, jdbc_catalog);
035  }
036
037
038/** {@see ConnectionMgr} **/
039public SimpleConnectionMgr(PropertyMgr props) 
040throws Exception
041  {
042  super(props);
043  }
044
045/** {@see ConnectionMgr} **/
046public SimpleConnectionMgr(PropertyMgr props, String prefix) 
047throws Exception
048  {
049  super(props, prefix);
050  }
051
052/** {@see ConnectionMgr} **/
053public SimpleConnectionMgr(SystemLog log, PropertyMgr props) 
054throws Exception
055  {
056  super(log, props);
057  }
058
059protected boolean handleMgrShutdown()
060  {
061  //nothing to do
062  return true;
063  }
064  
065/**
066Returns a new connection to the DB everytime it's called.
067**/ 
068protected Connection getConImpl() throws SQLException
069  {
070  Connection con = null;
071  log.bug("enter");
072  if (log.getLevel() == Log.DEBUG) {
073    DriverManager.setLogStream(System.err);
074    }
075  try {
076    con = DriverManager.getConnection(url,user,password);
077    log.bug("getOneImpl(), got connection: " + con);  
078    }
079  catch (SQLException e) {  
080    log.warn("getOneConnection(): Could not get a database connection.", e);
081    String socksProxyHost = System.getProperty("socksProxyHost");
082    if (socksProxyHost != null) 
083      { 
084      log.warn("A network proxy is in use (this could be the reason): ",
085        "socksProxyHost=", socksProxyHost,
086            ", socksProxyPort=", System.getProperty("socksProxyPort"));
087          log.warn("Invoke java with -DsocksProxyHost='' to get rid of the proxy");
088          }
089    e.fillInStackTrace();
090    throw e;
091    }
092  return con;
093  } //~ connect()
094    
095/** 
096Unit Test History   
097<pre>
098Tester    Status      Notes       
099hj      passed    
100</pre>
101**/ 
102public static void main(String[] args) throws Exception
103  {
104  Args myargs = new Args(args);
105  myargs.setUsage("java fc.jdbc.SimpleConnectionMgr -conf <path-to-config-file> [-query query-to-execute]");
106  
107  String query    = myargs.get("query", null);
108  String propfile = myargs.getRequired("conf");
109  
110  try {
111    ConnectionMgr mgr = new SimpleConnectionMgr(  
112                  new FilePropertyMgr(
113                    new File(propfile)));
114    
115    Connection con = mgr.getConnection();
116    System.out.println("GOT CONNECTION:" + con);
117
118    if (query != null)
119      {
120      Statement stmt = con.createStatement();
121      ResultSet rs = stmt.executeQuery(query);
122      System.out.println("======= query using connection ===");
123      QueryUtil.printRS(rs);
124      System.out.println("==================================");
125      stmt.close();   
126      }
127    System.out.println("The following should give an error");
128    mgr.close();
129    mgr.getConnection();
130    }
131  catch (Exception e) {
132    e.printStackTrace();
133    } 
134  }
135  
136}  //~class JDBCConnection
137
138