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
 *
8
 *   '$Author: jones $'
9
 *     '$Date: 2006-11-10 11:30:36 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3080 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27

    
28

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

    
35

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

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

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

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