Example usage for java.sql PreparedStatement isClosed

List of usage examples for java.sql PreparedStatement isClosed

Introduction

In this page you can find the example usage for java.sql PreparedStatement isClosed.

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this Statement object has been closed.

Usage

From source file:org.openbravo.service.system.SystemService.java

private void killConnectionsAndSafeMode(Connection con) {
    try {/*from   w w w.j av  a 2s .  c om*/
        PreparedStatement updateSession = null;
        try {
            updateSession = con.prepareStatement("UPDATE AD_SESSION SET SESSION_ACTIVE='N' WHERE CREATEDBY<>?");
            updateSession.setString(1, OBContext.getOBContext().getUser().getId());
            updateSession.executeUpdate();
        } finally {
            if (updateSession != null && !updateSession.isClosed()) {
                updateSession.close();
            }
        }
        PreparedStatement ps2 = null;
        try {
            ps2 = con.prepareStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='RB80'");
            ps2.executeUpdate();
        } finally {
            if (ps2 != null && !ps2.isClosed()) {
                ps2.close();
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Couldn't destroy concurrent sessions", e);
    }
}

From source file:org.openmrs.web.filter.util.FilterUtil.java

/**
 * Tries to retrieve location parameter. First this method makes an attempt to load locale
 * parameter as user's property. And next, if user's property is empty it tries to retrieve
 * default system locale (i.e system global property). If it also is empty it uses default value
 * for system locale//w  w  w.java2  s  . c  om
 *
 * @param username the name of the administrative user whose default locale property will be
 *            restored
 * @return string with stored location parameter or default OpenMRS locale property's value
 */
public static String restoreLocale(String username) {
    String currentLocale = null;
    if (StringUtils.isNotBlank(username)) {
        PreparedStatement statement = null;
        Connection connection = null;
        try {
            connection = DatabaseUpdater.getConnection();

            // first we should try to get locale parameter as user's property
            Integer userId = getUserIdByName(username, connection);

            if (userId != null) {
                String select = "select property_value from user_property where user_id = ? and property = ?";
                statement = connection.prepareStatement(select);
                statement.setInt(1, userId);
                statement.setString(2, OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE);
                if (statement.execute()) {
                    ResultSet results = statement.getResultSet();
                    if (results.next()) {
                        currentLocale = results.getString(1);
                    }
                }

                //close statement
                statement.close();
            }

            // if locale is still null we should try to retrieve system locale global property's value
            if (currentLocale == null) {
                currentLocale = readSystemDefaultLocale(connection);
            }
        } catch (Exception e) {
            log.error("Error while retriving locale property", e);
        } finally {
            try {
                if (statement != null && !statement.isClosed()) {
                    statement.close();
                }
            } catch (SQLException e) {
                log.warn("Error while closing statement");
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    log.debug("Error while closing the database", e);
                }
            }
        }
    }
    // if locale is still null we just simply using default locale value (i.e. en_GB)
    if (currentLocale == null) {
        currentLocale = OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE;
    }

    return currentLocale;
}

From source file:org.sakaiproject.lap.dao.Database.java

/**
 * Closes a prepared statement, after returning the borrowed connection
 * //from w ww.  j  a  va2  s  .  c om
 * @param preparedStatement the prepared statement
 */
protected void closePreparedStatement(PreparedStatement preparedStatement) {
    try {
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            try {
                returnConnection(preparedStatement.getConnection());
            } catch (Exception e) {
                log.error("Error returning connection to pool: " + e, e);
            } finally {
                preparedStatement.close();
            }
        }
    } catch (Exception e) {
        log.error("Error closing prepared statement: " + e, e);
    }
}

From source file:org.sakaiproject.oaai.dao.Db.java

protected void closePreparedStatement(PreparedStatement preparedStatement) {
    try {//  ww w .ja v  a2 s.  c  o  m
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            try {
                returnConnection(preparedStatement.getConnection());
            } catch (Exception e) {
                log.error("Error returning connection to pool: " + e, e);
            } finally {
                preparedStatement.close();
            }
        }
    } catch (Exception e) {
        log.error("Error closing prepared statement: " + e, e);
    }
}

From source file:org.sonar.server.db.ResultSetIteratorTest.java

@Test
public void create_iterator_from_statement() throws Exception {
    dbTester.prepareDbUnit(getClass(), "feed.xml");

    PreparedStatement stmt = connection.prepareStatement("select * from fake order by id");
    FirstIntColumnIterator iterator = new FirstIntColumnIterator(stmt);

    assertThat(iterator.hasNext()).isTrue();

    // calling multiple times hasNext() is ok
    assertThat(iterator.hasNext()).isTrue();

    assertThat(iterator.next()).isEqualTo(10);
    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next()).isEqualTo(20);

    // call next() without calling hasNext()
    assertThat(iterator.next()).isEqualTo(30);
    assertThat(iterator.hasNext()).isFalse();

    try {/*from ww  w . j a  v  a 2s .c o  m*/
        iterator.next();
        fail();
    } catch (NoSuchElementException e) {
        // ok
    }

    iterator.close();
    // statement is closed by ResultSetIterator
    assertThat(stmt.isClosed()).isTrue();
}