KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > database > PoolTestThread


1
2 package org.jahia.services.database;
3
4 import java.util.*;
5 import java.sql.*;
6
7 public class PoolTestThread implements Runnable JavaDoc {
8
9     private static org.apache.log4j.Logger logger =
10             org.apache.log4j.Logger.getLogger(PoolTestThread.class);
11
12     ConnectionPool connectionPool;
13     int tries;
14     boolean success = true;
15     Random myRandom;
16     private int maxSleepTime;
17
18     public PoolTestThread(ConnectionPool connectionPool, Random myRandom, int maxSleepTime) {
19         this.connectionPool = connectionPool;
20         this.myRandom = myRandom;
21         tries = myRandom.nextInt(30);
22         this.maxSleepTime = maxSleepTime;
23     }
24
25     private boolean makeQuery() {
26         try {
27             int debugID1 = 10;
28             int debugID2 = 20;
29             Connection connectionRet = connectionPool.getConnection(debugID1);
30             connectionRet.setAutoCommit(false);
31             if (connectionRet != null) {
32                 PreparedStatement stmt = connectionRet.prepareStatement("SELECT * FROM JAHIA_TEST WHERE TEST=?");
33                 stmt.setString(1, "test");
34                 ResultSet rs = stmt.executeQuery();
35                 while (rs.next()) {
36                     String JavaDoc curTest = rs.getString("test");
37                 }
38                 stmt.close();
39                 stmt = connectionRet.prepareStatement("INSERT INTO JAHIA_TEST VALUES (?)");
40                 stmt.setString(1, "test");
41                 stmt.execute();
42                 stmt.close();
43                 connectionRet.commit();
44                 connectionRet.setAutoCommit(true);
45                 connectionPool.free(connectionRet);
46             }
47             connectionRet = connectionPool.getConnection(debugID1);
48             connectionRet.setAutoCommit(false);
49             if (connectionRet != null) {
50                 PreparedStatement stmt = connectionRet.prepareStatement("UPDATE JAHIA_TEST SET TEST=? WHERE TEST=?");
51                 stmt.setString(1, "test2");
52                 stmt.setString(2, "test");
53                 stmt.execute();
54                 stmt.close();
55                 stmt = connectionRet.prepareStatement("DELETE FROM JAHIA_TEST");
56                 stmt.execute();
57                 stmt.close();
58                 connectionRet.commit();
59                 connectionRet.setAutoCommit(true);
60                 connectionPool.free(connectionRet);
61                 return true;
62             }
63         } catch(Exception JavaDoc e) {
64             e.printStackTrace();
65         }
66         return false;
67     }
68
69     public void setUp() {
70     }
71
72     public void tearDown() {
73     }
74
75     public void run() {
76         logger.debug("Executing " + tries + " queries...");
77         for (int i=0; i < tries; i++) {
78             try {
79                 long sleepTime = myRandom.nextInt(maxSleepTime);
80                 logger.debug("Sleeping " + sleepTime + "ms...");
81                 Thread.sleep(sleepTime);
82             } catch (InterruptedException JavaDoc ie) {
83                 logger.debug("Interrupted while sleeping !", ie);
84             }
85             boolean result = makeQuery();
86             if (!result) {
87                 success = false;
88             }
89         }
90     }
91
92     public boolean getResult () {
93         return success;
94     }
95
96 }
97
Popular Tags