Multi - Threaded JDBC Connections

From Ingres Community Wiki

Jump to: navigation, search
/*
** This program connects to a database and runs a simple query in
** a multi-threaded context.  It can be used for stress testing.
*/
import java.util.*;      
import java.io.*;
import java.sql.*;
import com.ingres.jdbc.*;
    
public class myThread extends Thread  
{
    public void run()
    {
        IngresDriver drv = null;
        String query = "select table_name from iitables";
        Connection con[] = new Connection[60];
         Statement s; 
        /*
        ** Put your own url in here.
        */
        String url = "jdbc:ingres://myhostname:port/myDb;UID=myUID;PWD=myPWD";
        System.out.println("Connecting to " + url);
        try 
        {
            drv = 
               (IngresDriver) Class.forName("com.ingres.jdbc.IngresDriver").newInstance();
        } 
        catch(java.lang.ClassNotFoundException e) 
        {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        } 
        catch (java.lang.IllegalAccessException ex)
        {
            ex.printStackTrace();
        }
        catch (java.lang.InstantiationException ex)
        {
            ex.printStackTrace();
        }
        try 
        {
            for (int i = 0; i < 4; i++)
            {
                 System.out.println("Getting the connection");
                 con[i] = DriverManager.getConnection(url); 
                 if (con[i] == null)
                     System.out.println("NO connection");
                 else
                     System.out.println("Connected at " + con[i]);
                 Statement stmt = con[i].createStatement();
                 ResultSet rs = stmt.executeQuery ("select table_name from iitables");
                 /*
                 ** For this test, it is not necessary to display the results of the fetch.
                 */
                 while (rs.next())
                 {
                     ;
                 }
             }
             for (int i = 0; i < 4; i++)
             {
                 System.out.println("Disconnecting " + i);
                 con[i].close();
             }
        } 
        catch(SQLException ex) 
        {
             System.err.print("SQLException: ");
             System.err.println(ex.getMessage());
        }
        System.out.println("");
        System.out.println("DONE!");
        System.out.println("");
    }
    public static void main(String args[]) 
    {
         for (int i = 0; i < 10; i++)
         {
             myThread m = new myThread();
             m.start();
         }
    }
} /* end myThread */
Personal tools
Developing With