We help IT Professionals succeed at work.
Get Started

Connection pool connecting to incoming data

gbcbr
gbcbr asked
on
407 Views
Last Modified: 2012-05-10
I found very simple solution for organizing connection pool at http://www.javareference.com/jrexamples/viewexample.jsp?id=41 and include it in my code.
Connection pool creates 15 connections and wait for data.
run:
          dbFX data started
=>
          Started at  : Sun Dec 05 16:48:37 EET 2010
=>
Driver   oracle.jdbc.OracleDriver
Checking out...oracle.jdbc.driver.T4CConnection@68814013
Available Connections ... 14
Checking out...oracle.jdbc.driver.T4CConnection@31884174
Available Connections ... 13
Checking out...oracle.jdbc.driver.T4CConnection@679bfb30
Available Connections ... 12
Checking out...oracle.jdbc.driver.T4CConnection@15f48262
Available Connections ... 11
Checking out...oracle.jdbc.driver.T4CConnection@9a68ef9
Available Connections ... 10
Checking out...oracle.jdbc.driver.T4CConnection@2937d090
Available Connections ... 9
Checking out...oracle.jdbc.driver.T4CConnection@5b976011
Available Connections ... 8
Checking out...oracle.jdbc.driver.T4CConnection@33aae94f
Available Connections ... 7
Checking out...oracle.jdbc.driver.T4CConnection@6a5f6303
Available Connections ... 6
Checking out...oracle.jdbc.driver.T4CConnection@7b5a6029
Available Connections ... 5
Checking out...oracle.jdbc.driver.T4CConnection@3d3e58d4
Available Connections ... 4
Checking out...oracle.jdbc.driver.T4CConnection@8888e6c
Available Connections ... 3
Checking out...oracle.jdbc.driver.T4CConnection@39e57e8f
Available Connections ... 2
Checking out...oracle.jdbc.driver.T4CConnection@30e3c624
Available Connections ... 1
Checking out...oracle.jdbc.driver.T4CConnection@39e87719
Available Connections ... 0
Checked in...oracle.jdbc.driver.T4CConnection@68814013
Available Connections ... 1
Checked in...oracle.jdbc.driver.T4CConnection@31884174
Available Connections ... 2
Checked in...oracle.jdbc.driver.T4CConnection@679bfb30
Available Connections ... 3
Checked in...oracle.jdbc.driver.T4CConnection@15f48262
Available Connections ... 4
Checked in...oracle.jdbc.driver.T4CConnection@9a68ef9
Available Connections ... 5
Checked in...oracle.jdbc.driver.T4CConnection@2937d090
Available Connections ... 6
Checked in...oracle.jdbc.driver.T4CConnection@5b976011
Available Connections ... 7
Checked in...oracle.jdbc.driver.T4CConnection@33aae94f
Available Connections ... 8
Checked in...oracle.jdbc.driver.T4CConnection@6a5f6303
Available Connections ... 9
Checked in...oracle.jdbc.driver.T4CConnection@7b5a6029
Available Connections ... 10
Checked in...oracle.jdbc.driver.T4CConnection@3d3e58d4
Available Connections ... 11
Checked in...oracle.jdbc.driver.T4CConnection@8888e6c
Available Connections ... 12
Checked in...oracle.jdbc.driver.T4CConnection@39e57e8f
Available Connections ... 13
Checked in...oracle.jdbc.driver.T4CConnection@30e3c624
Available Connections ... 14
Checked in...oracle.jdbc.driver.T4CConnection@39e87719
Available Connections ... 15
          dbFX data server 
=>
CLEANUP : Available Connections : 11
          dbFX: started logging in
=>
>>> requestTradingSessionStatus = DB_U100D1_jAIAi7bDs9tTJLFRni5qjj7tRaot8LyhJkCqGvgaGDSr2LNx117ZDr-8
=>
          dbFX: done logging in at : Sun Dec 05 16:48:57 EET 2010

Open in new window

But the problem is that into the class Pool_Init I have  
public class Pool_Init {

    private static String driverName = "oracle.jdbc.OracleDriver";

    public static void init (String[] args) {

        try
        {
            Class.forName(driverName).newInstance();
            System.out.println("Driver   " + driverName);
        }
        catch (Exception E)
        {
            System.err.println("Unable to load driver.");
            E.printStackTrace();
        }

        try
        {
            ConnectionPool cp = new ConnectionPool("jdbc:oracle:thin:@10.1.1.7:1521:orcl", "", "");

            Connection []connArr = new Connection[15];

            for(int i=0; i<connArr.length;i++)
            {
                connArr[i] = cp.checkout();
                System.out.println("Checking out..." + connArr[i]);
                System.out.println("Available Connections ... " + cp.availableCount());
            }

            for(int i=0; i<connArr.length;i++)
            {
                cp.checkin(connArr[i]);
                System.out.println("Checked in..." + connArr[i]);
                System.out.println("Available Connections ... " + cp.availableCount());
            }
        }
        catch(SQLException sqle)
        {
            sqle.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Open in new window

Connection []connArr is an array and when I try to use this connection into ConnectDB class
public class ConnectDB {

    Statement statement;
    Connection conn;
    Pool_Init pi = new Pool_Init();
    Connection[] connArr;

    public void insert(String[] args, String sql) throws SQLException {

        pi.init(args);
        statement = (Statement) connArr.createStatement();
        int st = statement.executeUpdate(sql);

        System.out.println("insert =>  " + sql + " : " + new java.util.Date());
        System.out.println();

        statement.close();
    }
}

Open in new window

I have error
statement = (Statement) connArr.createStatement();
cannot find symbol
symbol: method createStatement()
location: class java.sql. Connection[]
As I understood this java class has not method with array.
Please advice how to add to this class method Connection[] or some other way to solve this question.
Thank you
Comment
Watch Question
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 44 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE