001: // JdbcConnection.java
002: // $Id: JdbcConnection.java,v 1.15 2001/03/13 14:08:46 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.jdbc;
007:
008: import java.io.PrintStream;
009:
010: import java.sql.Connection;
011: import java.sql.DatabaseMetaData;
012: import java.sql.DriverManager;
013: import java.sql.ResultSet;
014: import java.sql.SQLException;
015: import java.sql.Statement;
016:
017: import org.w3c.util.LRUAble;
018:
019: public class JdbcConnection implements LRUAble {
020:
021: public static final boolean debug = false;
022:
023: public static final int QUERY_TIMEOUT = 5 * 60; // 5 minutes
024:
025: protected long querystamp = -1;
026:
027: Connection conn = null;
028:
029: // LRUAble interface implementation.
030: protected LRUAble lru_next = null;
031: protected LRUAble lru_prev = null;
032: /**
033: * The server this connection is attached to.
034: */
035: protected JdbcServer server = null;
036:
037: /**
038: * LRUAble interface - Get previous item in the LRU list.
039: * @return The previous item, or <strong>null</strong>.
040: */
041:
042: public LRUAble getNext() {
043: return lru_next;
044: }
045:
046: /**
047: * LRUAble interface - Get next item in the LRU list.
048: * @return The next item, or <strong>null</strong>.
049: */
050:
051: public LRUAble getPrev() {
052: return lru_prev;
053: }
054:
055: /**
056: * LRUAble interface - Set the next item for this server.
057: */
058:
059: public void setNext(LRUAble next) {
060: lru_next = next;
061: }
062:
063: /**
064: * LRUAble interface - Set the previous item for this server.
065: */
066:
067: public void setPrev(LRUAble prev) {
068: lru_prev = prev;
069: }
070:
071: protected final JdbcServer getServer() {
072: return server;
073: }
074:
075: /**
076: * Mark the connection used.
077: * @return true if the connection is usable, false otherwise.
078: */
079: public synchronized boolean markUsed() {
080: try {
081: if (conn != null && conn.isClosed()) {
082: try {
083: conn.close();
084: } catch (Exception ex) {
085: }
086: ; // just in case
087: conn = null;
088: }
089: } catch (SQLException sqlex) {
090: // isClosed was not possible...
091: try {
092: conn.close();
093: } catch (Exception ex) {
094: }
095: ; // just in case
096: conn = null;
097: }
098: if (conn == null) {
099: try {
100: DriverManager.setLoginTimeout(60); // 1 minute
101: conn = DriverManager.getConnection(server.uri,
102: server.user, server.password);
103: } catch (SQLException ex) {
104: if (debug) {
105: System.err.println(ex.getMessage());
106: }
107: // not good
108: server.unregisterConnection(this );
109: server.deleteConnection(this );
110: return false;
111: }
112: }
113: server.unregisterConnection(this );
114: return true;
115: }
116:
117: public ResultSet performQuery(String command) throws SQLException {
118: if (conn != null) {
119: try {
120: querystamp = System.currentTimeMillis();
121: Statement smt = conn.createStatement();
122: smt.setQueryTimeout(QUERY_TIMEOUT);
123: ResultSet set = smt.executeQuery(command);
124: return set;
125: } finally {
126: querystamp = -1;
127: }
128: }
129: throw new SQLException("no connection");
130: }
131:
132: public int performUpdate(String command) throws SQLException {
133: if (conn != null) {
134: try {
135: querystamp = System.currentTimeMillis();
136: Statement smt = conn.createStatement();
137: smt.setQueryTimeout(QUERY_TIMEOUT);
138: return smt.executeUpdate(command);
139: } finally {
140: querystamp = -1;
141: }
142: }
143: throw new SQLException("no connection");
144: }
145:
146: public long getQueryStamp() {
147: return querystamp;
148: }
149:
150: public DatabaseMetaData getMetaData() throws SQLException {
151: if (conn != null) {
152: return conn.getMetaData();
153: }
154: throw new SQLException("no connection");
155: }
156:
157: /**
158: * Close (if necessary) and delete this connection.
159: */
160: public void delete() {
161: close();
162: server.unregisterConnection(this );
163: server.deleteConnection(this );
164: }
165:
166: public void close() {
167: if (conn != null) {
168: try {
169: conn.close();
170: } catch (SQLException ex) {
171: // abort anyway
172: }
173: conn = null;
174: }
175: }
176:
177: public boolean isClosed() {
178: if (conn != null) {
179: try {
180: return conn.isClosed();
181: } catch (SQLException ex) {
182: return true;
183: }
184: }
185: return true;
186: }
187:
188: JdbcConnection(JdbcServer server) {
189: this.server = server;
190: }
191:
192: }
|