KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > PooledConnectionImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.jdbc;
31
32 import com.caucho.db.Database;
33 import com.caucho.util.L10N;
34
35 import javax.sql.ConnectionEvent JavaDoc;
36 import javax.sql.ConnectionEventListener JavaDoc;
37 import javax.sql.PooledConnection JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * The JDBC connection implementation.
44  */

45 class PooledConnectionImpl implements PooledConnection JavaDoc {
46   private final static L10N L = new L10N(PooledConnectionImpl.class);
47   
48   private final Database _db;
49   
50   private ArrayList JavaDoc<ConnectionEventListener JavaDoc> _listeners =
51     new ArrayList JavaDoc<ConnectionEventListener JavaDoc>();
52
53   private boolean _isClosed;
54   
55   PooledConnectionImpl(Database db)
56   {
57     _db = db;
58
59     if (_db == null)
60       throw new NullPointerException JavaDoc();
61   }
62
63   /**
64    * Returns the database.
65    */

66   Database getDatabase()
67   {
68     return _db;
69   }
70
71   /**
72    * Returns a new connection implementation.
73    */

74   public Connection JavaDoc getConnection()
75     throws SQLException JavaDoc
76   {
77     if (_isClosed)
78       throw new IllegalStateException JavaDoc(L.l("getting connection after close"));
79
80     // PooledConnectionImpl conn = new PooledConnectionImpl(this);
81

82     return new ConnectionImpl(this);
83   }
84
85   /**
86    * Adds a new listener.
87    */

88   public void addConnectionEventListener(ConnectionEventListener JavaDoc listener)
89   {
90     if (! _listeners.contains(listener))
91       _listeners.add(listener);
92   }
93
94   /**
95    * Removes the new listener.
96    */

97   public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener)
98   {
99     _listeners.remove(listener);
100   }
101
102   void closeEvent(Connection JavaDoc conn)
103   {
104     ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this);
105     
106     for (int i = 0; i < _listeners.size(); i++) {
107       ConnectionEventListener JavaDoc listener = _listeners.get(i);
108
109       listener.connectionClosed(event);
110     }
111   }
112
113   public void close()
114     throws SQLException JavaDoc
115   {
116     _isClosed = true;
117   }
118 }
119
Popular Tags