public ResultSet query(String sql) throws SQLException {
		if (debug) {
			System.out.println(sql);
		}
		SQLException exception = null;
		for (int i = MAX_QUERY_ATTEMPTS; --i >= 0;) {
			lock.lock();
			try {
				return stmtQuery.executeQuery(sql);
			} catch (SQLException ex) {
				if (isConnectionIssue(ex)) {
					exception = ex;
					reconnect();
				} else if (isSyntaxIssue(ex)) {
					System.err.println("Invalid SQL:\n" + sql);
					throw ex;
				} else {
					System.err.println("SQL Error: " + ex.getErrorCode() + " "
							+ ex.getSQLState());
					throw ex;
				}
			} finally {
				lock.unlock();
			}
		}
		throw exception;
	}