KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > ConnectionImpl


1 package example;
2
3 import java.io.*;
4 import java.util.*;
5
6 import javax.resource.*;
7 import javax.resource.spi.*;
8 import javax.security.auth.*;
9
10 /**
11  * Implementation of the user's view of the connection. This
12  * class is entirely customizable.
13  *
14  * Normally, it will just be a facade to the underlying managed
15  * connection.
16  */

17 public class ConnectionImpl {
18   private String JavaDoc _name;
19   
20   // Reference to the underlying connection
21
private ManagedConnectionImpl _mConn;
22
23   private volatile boolean _isClosed;
24
25   /**
26    * Create the connection, with a reference to the underlying
27    * connection.
28    */

29   ConnectionImpl(String JavaDoc name, ManagedConnectionImpl mConn)
30   {
31     _name = name;
32     _mConn = mConn;
33   }
34
35   /**
36    * Adding a <code>close()</code> method is very important for any
37    * connection API. It lets Resin know that the user has
38    * closed the connection and Resin can mark the managed connection
39    * as idle and reuse it.
40    *
41    * It is also important that the connection let the user call
42    * <code>close()</code>, but only the allow the first <code>close</code>
43    * to have any effect.
44    *
45    * In particular, it would be a mistake to call <code>_mConn.close</code>
46    * twice since that would tell Resin that the connection had closed
47    * twice, which might confuse Resin's pool.
48    */

49   public void close()
50   {
51     synchronized (this) {
52       if (_isClosed)
53     return;
54       _isClosed = true;
55     }
56
57     ManagedConnectionImpl mConn = _mConn;
58     _mConn = null;
59     
60     mConn.close(this);
61   }
62
63   public String JavaDoc toString()
64   {
65     return "ConnectionImpl[" + _name + "," + _mConn + "]";
66   }
67
68   public void finalize()
69   {
70     close();
71   }
72 }
73
Popular Tags