KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > GetConnectionThread


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.sql.Connection JavaDoc;
28
29 import junit.framework.Assert;
30
31 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
32 import org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager;
33
34 /**
35  * This thread gets a connection during a given time and releases it after.
36  *
37  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
38  */

39 public class GetConnectionThread extends Thread JavaDoc
40 {
41   /** Pool connection manager. */
42   private AbstractPoolConnectionManager pool;
43
44   /** Time in milliseconds the thread is using the connection. */
45   private long time;
46
47   /**
48    * Creates a new <code>GetConnectionThread</code> instance.
49    *
50    * @param name thread name.
51    * @param pool pool connection manager.
52    * @param time time in milliseconds the thread is using the connection.
53    */

54   public GetConnectionThread(String JavaDoc name, AbstractPoolConnectionManager pool,
55       long time)
56   {
57     super(name);
58     this.pool = pool;
59     this.time = time;
60   }
61
62   /**
63    * @see java.lang.Runnable#run()
64    */

65   public void run()
66   {
67     Connection JavaDoc c = null;
68     try
69     {
70       c = pool.getConnection();
71     }
72     catch (UnreachableBackendException e1)
73     {
74       Assert.fail("Backend unreachable during test.");
75     }
76     try
77     {
78       Thread.sleep(time);
79     }
80     catch (InterruptedException JavaDoc e)
81     {
82       Assert.fail("Exception thrown: " + e);
83     }
84     pool.releaseConnection(c);
85   }
86 }
Popular Tags