Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class to test how many sql command one connection can execute
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Jing Tao
7
 *    Release: @release@
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2002-06-13 12:01:29 -0700 (Thu, 13 Jun 2002) $'
11
 * '$Revision: 1218 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28

    
29

    
30
import java.io.*;
31
import java.util.Vector;
32
import java.lang.*;
33
import java.sql.*;
34
import edu.ucsb.nceas.metacat.*;
35

    
36

    
37
/**
38
 * A class to test when mamimum dbconnection number reached
39
 */
40
 
41
public class MaxmumDBConnection implements Runnable
42
{
43
 
44
  /**
45
   * Usage: java -cp metacat.jar   < -t times>
46
   *
47
   */
48
  public static void main(String [] args) throws Exception
49
  {
50
    // defaul loop value is 10
51
    int loop = 10;
52
    Thread runner = null;
53
       
54
    DBConnectionPool pool = DBConnectionPool.getInstance();
55

    
56
 
57
    // get loop number from argument
58
    for ( int i=0 ; i < args.length; ++i ) 
59
    {
60
       
61
      if ( args[i].equals( "-t" ) ) 
62
      {
63
          loop =  Integer.parseInt(args[++i]);
64
      }//if 
65
      else 
66
      {
67
          System.err.println("   args[" +i+ "] '" +args[i]+ "' ignored." );
68
      }//else
69
    }//for
70
      
71
    System.out.println("loop: "+loop);
72
      
73
    //ran the number of thread.
74
    for (int i= 0; i<loop; i++)
75
    {
76
          runner = new Thread( new MaxmumDBConnection());
77
          runner.start();
78
  
79
    }//for
80
    System.out.println("End");
81
     
82
   
83
   
84
  }//main
85
  
86

    
87
   
88
  
89
  /**
90
   * Method to run a sal insert command
91
   * @param conn, the connection will be used
92
   * @param i, part of docid
93
   */
94
  private static void insert(long i) throws SQLException
95
  {
96
   
97
    int serialNumber = 0;
98
    DBConnection conn = null;
99
    PreparedStatement pStmt = null;
100
    String sql = "insert into xml_documents (docid) values (?)";
101
    String docid = "jing."+i;
102
  
103
    try 
104
    {
105
      conn = DBConnectionPool.getDBConnection("insert");
106
     
107
      serialNumber = conn.getCheckOutSerialNumber();
108
      System.out.println("serialNumber: "+serialNumber);
109
      pStmt = conn.prepareStatement(sql);
110
      pStmt.setString(1, docid);
111
      pStmt.execute();
112
      System.out.println("Inserted successfully: "+i);
113
     }
114
     finally
115
     {
116
       try
117
       {
118
          pStmt.close();
119
       }
120
       finally
121
       {
122
          DBConnectionPool.returnDBConnection(conn, serialNumber);
123
          System.out.println("return inert conn!");
124
          System.out.println("the return insert connection's status: "
125
                                                 +conn.getStatus());
126
       }
127
     }
128
    
129
  }//insert
130
    
131
 
132
  /**
133
   * Method to run a sql select commnad
134
   * @param conn, connection will be used
135
   */
136
  private static void select() throws SQLException
137
  {
138
   
139
    int serialNumber = 0;
140
    DBConnection conn = null;
141
    PreparedStatement pStmt = null;
142
    ResultSet rs = null;
143
    String sql = "select docid from xml_documents where docid like'%jing%'";
144
    
145
    try
146
    {
147
      conn = DBConnectionPool.getDBConnection("select");
148
      serialNumber = conn.getCheckOutSerialNumber();
149
      System.out.println("serialNumber: "+serialNumber);
150
      pStmt = conn.prepareStatement(sql);
151
      pStmt.execute();
152
      rs = pStmt.getResultSet();
153
      if (rs.next())
154
      {
155
        String str = rs.getString(1);
156
        System.out.println("Select docid: "+str);
157
      }//if
158
    }//try
159
    finally
160
    {
161
      try
162
      {
163
        pStmt.close();
164
      }//try
165
      finally
166
      {
167
        DBConnectionPool.returnDBConnection(conn,serialNumber);
168
        System.out.println("return select conn!");
169
        System.out.println("the return insert connection's status: "
170
                                                        +conn.getStatus());
171
      }//finally
172

    
173
    }//fianlly
174
    
175
  }//select
176
  
177
  public void run()
178
  {
179
     long index = (new Double (Math.random()*100000)).longValue();
180
     try
181
     {
182
       insert(index);
183
       select();
184
     }//try
185
     catch (SQLException e)
186
     {
187
       System.out.println("error is run: "+e.getMessage());
188
     }
189
     
190
  }
191
     
192
  
193
}//DBConnectionPoolTester
(2-2/30)