Example usage for java.sql Connection setReadOnly

List of usage examples for java.sql Connection setReadOnly

Introduction

In this page you can find the example usage for java.sql Connection setReadOnly.

Prototype

void setReadOnly(boolean readOnly) throws SQLException;

Source Link

Document

Puts this connection in read-only mode as a hint to the driver to enable database optimizations.

Usage

From source file:ua.utility.kfsdbupgrade.App.java

/**
 * /*from w ww .  j  av  a2  s .  c om*/
 * @return {@link Connection} to the database with the URL of
 *         <code>database-url</code> in this {@link App}s {@link Properties}
 *         . Uses the <code>database-user</code>,
 *         <code>database-password</code>, <code>database-name</code>, and
 *         <code>database-driver</code> {@link Properties} entries for
 *         connection details. Read-only and auto-commit for the
 *         {@link Connection} are both set to <code>false</code>.
 * @throws Exception
 *             Any {@link Exception}s encountered will be rethrown.
 */
protected Connection getUpgradeConnection() throws Exception {
    Connection retval = null;
    String url = properties.getProperty("database-url");

    Properties props = new Properties();
    props.setProperty("user", properties.getProperty("database-user"));
    props.setProperty("password", properties.getProperty("database-password"));

    LOGGER.info("Connecting to db " + properties.getProperty("database-name") + "...");
    LOGGER.info("url=" + url);

    Class.forName(properties.getProperty("database-driver"));
    retval = DriverManager.getConnection(url, props);
    retval.setReadOnly(false);
    retval.setAutoCommit(false);

    LOGGER.info("connected to database " + properties.getProperty("database-name"));
    LOGGER.info("");

    return retval;
}

From source file:weave.utils.SQLUtils.java

/**
 * This function returns a read-only connection that can be reused.  The connection should not be closed.
 * @param driver An SQL driver to use.//from   ww  w . j a v  a 2  s  . c o m
 * @param connectString The connect string used to create the Connection.
 * @return A static read-only Connection.
 */
public static Connection getStaticReadOnlyConnection(String driver, String connectString)
        throws RemoteException {
    synchronized (_staticReadOnlyConnections) {
        Connection conn = null;
        if (_staticReadOnlyConnections.containsKey(connectString)) {
            conn = _staticReadOnlyConnections.get(connectString);
            if (connectionIsValid(conn))
                return conn;
            // if connection is not valid, remove this entry from the Map
            _staticReadOnlyConnections.remove(connectString);
        }

        // get a new connection, throwing an exception if this fails
        conn = getConnection(driver, connectString);

        // try to set readOnly.. if this fails, continue anyway.
        try {
            conn.setReadOnly(true);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // remember this static, read-only connection.
        if (conn != null)
            _staticReadOnlyConnections.put(connectString, conn);

        return conn;
    }
}