List of usage examples for java.sql Connection isReadOnly
boolean isReadOnly() throws SQLException;
Connection
object is in read-only mode. From source file:org.cfr.capsicum.datasource.DataSourceUtils.java
/** * Reset the given Connection after a transaction, * regarding read-only flag and isolation level. * @param con the Connection to reset//from w ww .j a va 2 s. c o m * @param previousIsolationLevel the isolation level to restore, if any * @see #prepareConnectionForTransaction */ public static void resetConnectionAfterTransaction(Connection con, Integer previousIsolationLevel) { Assert.notNull(con, "No Connection specified"); try { // Reset transaction isolation to previous value, if changed for the transaction. if (previousIsolationLevel != null) { if (logger.isDebugEnabled()) { logger.debug("Resetting isolation level of JDBC Connection [" + con + "] to " + previousIsolationLevel); } con.setTransactionIsolation(previousIsolationLevel.intValue()); } // Reset read-only flag. if (con.isReadOnly()) { if (logger.isDebugEnabled()) { logger.debug("Resetting read-only flag of JDBC Connection [" + con + "]"); } con.setReadOnly(false); } } catch (Throwable ex) { logger.debug("Could not reset JDBC Connection after transaction", ex); } }
From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java
@Test public void testInfo() throws Exception { Connection conn = new MyProxy(); try {/*w w w .ja va 2s . c om*/ try { conn.getMetaData(); } catch (SQLException e) { } try { conn.setCatalog(conn.getCatalog()); } catch (SQLException e) { } try { conn.setReadOnly(conn.isReadOnly()); } catch (SQLException e) { } try { conn.setTransactionIsolation(conn.getTransactionIsolation()); } catch (SQLException e) { } try { conn.setTransactionIsolation(conn.getTransactionIsolation()); } catch (SQLException e) { } try { conn.getWarnings(); } catch (SQLException e) { } try { conn.clearWarnings(); } catch (SQLException e) { } try { conn.setHoldability(conn.getHoldability()); } catch (SQLException e) { } try { conn.setSchema(conn.getSchema()); } catch (SQLException e) { } } finally { JdbcUtil.closeQuietly(conn); } }
From source file:helma.objectmodel.db.NodeManager.java
/** * Performs the actual deletion of a node from either the embedded or an external * SQL database./* w ww . ja va 2s .com*/ */ public void deleteNode(IDatabase db, ITransaction txn, Node node) throws Exception { DbMapping dbm = node.getDbMapping(); if ((dbm == null) || !dbm.isRelational()) { db.deleteNode(txn, node.getID()); } else { Statement st = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String str = new StringBuffer("DELETE FROM ").append(dbm.getTableName()).append(" WHERE ") .append(dbm.getIDField()).append(" = ").append(node.getID()).toString(); try { Connection con = dbm.getConnection(); // set connection to write mode if (con.isReadOnly()) con.setReadOnly(false); st = con.createStatement(); st.executeUpdate(str); } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL DELETE", dbm.getTableName(), logTimeStart, logTimeStop, str); } if (st != null) { try { st.close(); } catch (Exception ignore) { } } } } // node may still be cached via non-primary keys. mark as invalid node.setState(Node.INVALID); }
From source file:helma.objectmodel.db.NodeManager.java
/** * Generates an ID for the table by finding out the maximum current value *//*from ww w . ja va2 s . co m*/ synchronized String generateMaxID(DbMapping map) throws Exception { String retval = null; Statement stmt = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String q = new StringBuffer("SELECT MAX(").append(map.getIDField()).append(") FROM ") .append(map.getTableName()).toString(); try { Connection con = map.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(q); // check for empty table if (!rs.next()) { long currMax = map.getNewID(0); retval = Long.toString(currMax); } else { long currMax = rs.getLong(1); currMax = map.getNewID(currMax); retval = Long.toString(currMax); } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_MAX", map.getTableName(), logTimeStart, logTimeStop, q); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return retval; }
From source file:helma.objectmodel.db.NodeManager.java
/** * Count the nodes contained in the child collection of the home node * which is defined by Relation rel./*from ww w .jav a 2s. com*/ */ public int countNodes(Node home, Relation rel) throws Exception { DbMapping type = rel == null ? null : rel.otherType; if (type == null || !type.isRelational()) { // this should never be called for embedded nodes throw new RuntimeException("countNodes called for non-relational node " + home); } int retval = 0; Connection con = type.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); Statement stmt = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String query = null; try { StringBuffer b = rel.getCountSelect(); if (home.getSubnodeRelation() != null) { // use the manually set subnoderelation of the home node query = b.append(" ").append(home.getSubnodeRelation()).toString(); } else { // let relation object build the query rel.buildQuery(b, home, false, true); query = b.toString(); } stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); if (!rs.next()) { retval = 0; } else { retval = rs.getInt(1); } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_COUNT", type.getTableName(), logTimeStart, logTimeStop, query); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return (rel.maxSize > 0) ? Math.min(rel.maxSize, retval) : retval; }
From source file:helma.objectmodel.db.NodeManager.java
/** * Similar to getNodeIDs, but returns a List that contains the nodes property names instead of IDs *//*from w ww .j av a2s . co m*/ public Vector getPropertyNames(Node home, Relation rel) throws Exception { DbMapping type = rel == null ? null : rel.otherType; if (type == null || !type.isRelational()) { // this should never be called for embedded nodes throw new RuntimeException("getPropertyNames called for non-relational node " + home); } Vector retval = new Vector(); Connection con = rel.otherType.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); Statement stmt = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String query = null; try { // NOTE: we explicitly convert tables StringBuffer to a String // before appending to be compatible with JDK 1.3 StringBuffer b = rel.getNamesSelect(); if (home.getSubnodeRelation() != null) { b.append(" ").append(home.getSubnodeRelation()); } else { // let relation object build the query rel.buildQuery(b, home, true, false); } stmt = con.createStatement(); query = b.toString(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String n = rs.getString(1); if (n != null) { retval.add(n); } } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_ACCESSNAMES", type.getTableName(), logTimeStart, logTimeStop, query); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return retval; }
From source file:helma.objectmodel.db.NodeManager.java
/** * Loades subnodes via subnode relation. This is similar to getNodeIDs, but it * actually loades all nodes in one go, which is better for small node collections. * This method is used when xxx.loadmode=aggressive is specified. *//*from ww w. j a v a 2s. c om*/ public List getNodes(Node home, Relation rel) throws Exception { // This does not apply for groupby nodes - use getNodeIDs instead assert rel.groupby == null; if ((rel == null) || (rel.otherType == null) || !rel.otherType.isRelational()) { // this should never be called for embedded nodes throw new RuntimeException("getNodes called for non-relational node " + home); } List retval = new ArrayList(); DbMapping dbm = rel.otherType; Connection con = dbm.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); Statement stmt = con.createStatement(); DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); String query = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; try { StringBuffer b = dbm.getSelect(rel); if (home.getSubnodeRelation() != null) { b.append(home.getSubnodeRelation()); } else { // let relation object build the query rel.buildQuery(b, home, true, false); } query = b.toString(); if (rel.maxSize > 0) { stmt.setMaxRows(rel.maxSize); } ResultSet rs = stmt.executeQuery(query); while (rs.next()) { // create new Nodes. Node node = createNode(rel.otherType, rs, columns, 0); if (node == null) { continue; } Key primKey = node.getKey(); retval.add(new NodeHandle(primKey)); registerNewNode(node, null); fetchJoinedNodes(rs, joins, columns.length); } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_ALL", dbm.getTableName(), logTimeStart, logTimeStop, query); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return retval; }
From source file:helma.objectmodel.db.NodeManager.java
String generateSequenceID(DbMapping map) throws Exception { Statement stmt = null;/*from ww w. j a v a 2 s .c om*/ String retval = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String q; if (map.isOracle()) { q = new StringBuffer("SELECT ").append(map.getIDgen()).append(".nextval FROM dual").toString(); } else if (map.isPostgreSQL() || map.isH2()) { q = new StringBuffer("SELECT nextval('").append(map.getIDgen()).append("')").toString(); } else { throw new RuntimeException("Unable to generate sequence: unknown DB"); } try { Connection con = map.getConnection(); // TODO is it necessary to set connection to write mode here? if (con.isReadOnly()) con.setReadOnly(false); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(q); if (!rs.next()) { throw new SQLException("Error creating ID from Sequence: empty recordset"); } retval = rs.getString(1); } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_NEXTVAL", map.getTableName(), logTimeStart, logTimeStop, q); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return retval; }
From source file:helma.objectmodel.db.NodeManager.java
private Node getNodeByKey(ITransaction txn, DbKey key) throws Exception { // Note: Key must be a DbKey, otherwise will not work for relational objects Node node = null;/*from w w w.j av a 2s.c o m*/ DbMapping dbm = app.getDbMapping(key.getStorageName()); String kstr = key.getID(); if ((dbm == null) || !dbm.isRelational()) { node = (Node) db.getNode(txn, kstr); if ((node != null) && (dbm != null)) { node.setDbMapping(dbm); } } else { String idfield = dbm.getIDField(); Statement stmt = null; String query = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; try { Connection con = dbm.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); stmt = con.createStatement(); DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); StringBuffer b = dbm.getSelect(null).append("WHERE "); dbm.appendCondition(b, idfield, kstr); dbm.addJoinConstraints(b, " AND "); query = b.toString(); ResultSet rs = stmt.executeQuery(query); if (!rs.next()) { return null; } node = createNode(dbm, rs, columns, 0); fetchJoinedNodes(rs, joins, columns.length); if (rs.next()) { app.logError("Warning: More than one value returned for query " + query); } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_BYKEY", dbm.getTableName(), logTimeStart, logTimeStop, query); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { // ignore } } } } return node; }
From source file:helma.objectmodel.db.NodeManager.java
/** * Loades subnodes via subnode relation. Only the ID index is loaded, the nodes are * loaded later on demand./*from w w w . java 2s . c o m*/ */ public List getNodeIDs(Node home, Relation rel) throws Exception { DbMapping type = rel == null ? null : rel.otherType; if (type == null || !type.isRelational()) { // this should never be called for embedded nodes throw new RuntimeException("getNodeIDs called for non-relational node " + home); } List retval = new ArrayList(); // if we do a groupby query (creating an intermediate layer of groupby nodes), // retrieve the value of that field instead of the primary key Connection con = type.getConnection(); // set connection to read-only mode if (!con.isReadOnly()) con.setReadOnly(true); Statement stmt = null; long logTimeStart = logSql ? System.currentTimeMillis() : 0; String query = null; try { StringBuffer b = rel.getIdSelect(); if (home.getSubnodeRelation() != null) { // subnode relation was explicitly set query = b.append(" ").append(home.getSubnodeRelation()).toString(); } else { // let relation object build the query rel.buildQuery(b, home, true, false); query = b.toString(); } stmt = con.createStatement(); if (rel.maxSize > 0) { stmt.setMaxRows(rel.maxSize); } ResultSet result = stmt.executeQuery(query); // problem: how do we derive a SyntheticKey from a not-yet-persistent Node? Key k = (rel.groupby != null) ? home.getKey() : null; while (result.next()) { String kstr = result.getString(1); // jump over null values - this can happen especially when the selected // column is a group-by column. if (kstr == null) { continue; } // make the proper key for the object, either a generic DB key or a groupby key Key key = (rel.groupby == null) ? (Key) new DbKey(rel.otherType, kstr) : (Key) new SyntheticKey(k, kstr); retval.add(new NodeHandle(key)); // if these are groupby nodes, evict nullNode keys if (rel.groupby != null) { Node n = (Node) cache.get(key); if ((n != null) && n.isNullNode()) { evictKey(key); } } } } finally { if (logSql) { long logTimeStop = System.currentTimeMillis(); logSqlStatement("SQL SELECT_IDS", type.getTableName(), logTimeStart, logTimeStop, query); } if (stmt != null) { try { stmt.close(); } catch (Exception ignore) { } } } return retval; }