KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > com > caucho > sql > DBPoolEcmaWrap


1 /*
2  * Copyright (c) 1998-1999 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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  *
28  * $Id: DBPoolEcmaWrap.java,v 1.2 2004/09/29 00:13:04 cvs Exp $
29  */

30
31 package com.caucho.eswrap.com.caucho.sql;
32
33 import com.caucho.es.ESException;
34 import com.caucho.sql.DBPool;
35 import com.caucho.util.Exit;
36 import com.caucho.util.ExitListener;
37
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.sql.Statement JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 public class DBPoolEcmaWrap {
47   public static DBPool call(String JavaDoc name)
48     throws Exception JavaDoc
49   {
50     Context JavaDoc cxt = (Context JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env");
51     
52     return (DBPool) cxt.lookup(name);
53   }
54
55   public static Connection JavaDoc getConnection(DBPool pool)
56     throws SQLException JavaDoc
57   {
58     Connection JavaDoc conn = pool.getConnection();
59     
60     // Resin automatically reclaims connections when the script ends
61
Exit.addExit(exitConnection, conn);
62
63     return conn;
64   }
65
66   public static Statement JavaDoc createStatement(DBPool pool)
67     throws SQLException JavaDoc
68   {
69     Connection JavaDoc conn = getConnection(pool);
70     
71     Statement JavaDoc stmt = conn.createStatement();
72     
73     // Resin automatically reclaims statements when the script ends
74
Exit.addExit(exitStatement, stmt);
75
76     return stmt;
77   }
78
79   public static int executeUpdate(DBPool pool, String JavaDoc msg)
80     throws SQLException JavaDoc
81   {
82     Connection JavaDoc conn = pool.getConnection();
83
84     try {
85       Statement JavaDoc stmt = conn.createStatement();
86
87       try {
88     return stmt.executeUpdate(msg);
89       } finally {
90     stmt.close();
91       }
92     } finally {
93       conn.close();
94     }
95   }
96
97   public static Object JavaDoc executeQuery(DBPool pool, String JavaDoc msg)
98     throws ESException, SQLException JavaDoc
99   {
100     Connection JavaDoc conn = pool.getConnection();
101
102     try {
103       Statement JavaDoc stmt = conn.createStatement();
104       ArrayList JavaDoc array = new ArrayList JavaDoc();
105
106       try {
107     ResultSet JavaDoc rs = stmt.executeQuery(msg);
108
109     try {
110       int i = 0;
111       while (rs.next()) {
112         // array.add(ResultSetEcmaWrap.toObject(rs, null));
113
}
114       return array.toArray(new Object JavaDoc[array.size()]);
115     } finally {
116       rs.close();
117     }
118       } finally {
119     stmt.close();
120       }
121     } finally {
122       conn.close();
123     }
124   }
125
126   private static ExitListener exitConnection = new ExitListener() {
127     public void handleExit(Object JavaDoc o)
128     {
129       Connection JavaDoc conn = (Connection JavaDoc) o;
130
131       try {
132     conn.close();
133       } catch (SQLException JavaDoc e) {
134       }
135     }
136   };
137
138   private static ExitListener exitStatement = new ExitListener() {
139     public void handleExit(Object JavaDoc o)
140     {
141       Statement JavaDoc stmt = (Statement JavaDoc) o;
142
143       try {
144     stmt.close();
145       } catch (SQLException JavaDoc e) {
146       }
147     }
148   };
149 }
150
Popular Tags