Example usage for java.sql PreparedStatement setQueryTimeout

List of usage examples for java.sql PreparedStatement setQueryTimeout

Introduction

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

Prototype

void setQueryTimeout(int seconds) throws SQLException;

Source Link

Document

Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds.

Usage

From source file:org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager.java

/**
 *
 *//*from  w w  w  .  j  a  va 2s  .  c  o  m*/
public String[] doListUsers(String filter, int maxItemLimit) throws UserStoreException {

    String[] users = new String[0];
    Connection dbConnection = null;
    String sqlStmt = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;

    if (maxItemLimit == 0) {
        return new String[0];
    }

    int givenMax = UserCoreConstants.MAX_USER_ROLE_LIST;

    int searchTime = UserCoreConstants.MAX_SEARCH_TIME;

    try {
        givenMax = Integer.parseInt(
                realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_USER_LIST));
    } catch (Exception e) {
        givenMax = UserCoreConstants.MAX_USER_ROLE_LIST;
    }

    try {
        searchTime = Integer.parseInt(
                realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME));
    } catch (Exception e) {
        searchTime = UserCoreConstants.MAX_SEARCH_TIME;
    }

    if (maxItemLimit < 0 || maxItemLimit > givenMax) {
        maxItemLimit = givenMax;
    }

    try {

        if (filter != null && filter.trim().length() != 0) {
            filter = filter.trim();
            filter = filter.replace("*", "%");
            filter = filter.replace("?", "_");
        } else {
            filter = "%";
        }

        List<String> lst = new LinkedList<String>();

        dbConnection = getDBConnection();

        if (dbConnection == null) {
            throw new UserStoreException("null connection");
        }

        if (isCaseSensitiveUsername()) {
            sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USER_FILTER);
        } else {
            sqlStmt = realmConfig.getUserStoreProperty(JDBCRealmConstants.GET_USER_FILTER_CASE_INSENSITIVE);
        }

        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setString(1, filter);
        if (sqlStmt.contains(UserCoreConstants.UM_TENANT_COLUMN)) {
            prepStmt.setInt(2, tenantId);
        }
        prepStmt.setMaxRows(maxItemLimit);
        try {
            prepStmt.setQueryTimeout(searchTime);
        } catch (Exception e) {
            // this can be ignored since timeout method is not implemented
            log.debug(e);
        }

        try {
            rs = prepStmt.executeQuery();
        } catch (SQLException e) {
            if (e instanceof SQLTimeoutException) {
                log.error("The cause might be a time out. Hence ignored", e);
                return users;
            }
            String errorMessage = "Error while fetching users according to filter : " + filter
                    + " & max Item limit " + ": " + maxItemLimit;
            if (log.isDebugEnabled()) {
                log.debug(errorMessage, e);
            }
            throw new UserStoreException(errorMessage, e);
        }

        while (rs.next()) {

            String name = rs.getString(1);
            if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equals(name)) {
                continue;
            }
            // append the domain if exist
            String domain = realmConfig
                    .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
            name = UserCoreUtil.addDomainToName(name, domain);
            lst.add(name);
        }
        rs.close();

        if (lst.size() > 0) {
            users = lst.toArray(new String[lst.size()]);
        }

        Arrays.sort(users);

    } catch (SQLException e) {
        String msg = "Error occurred while retrieving users for filter : " + filter + " & max Item limit : "
                + maxItemLimit;
        if (log.isDebugEnabled()) {
            log.debug(msg, e);
        }
        throw new UserStoreException(msg, e);
    } finally {
        DatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
    }
    return users;

}

From source file:org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager.java

private void setPSRestrictions(PreparedStatement ps, int maxItemLimit) throws SQLException {

    int givenMax = UserCoreConstants.MAX_USER_ROLE_LIST;

    int searchTime = UserCoreConstants.MAX_SEARCH_TIME;

    try {/*from w ww .j  a v a2s  .c o m*/
        givenMax = Integer.parseInt(
                realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_ROLE_LIST));
    } catch (Exception e) {
        givenMax = UserCoreConstants.MAX_USER_ROLE_LIST;
    }

    try {
        searchTime = Integer.parseInt(
                realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_MAX_SEARCH_TIME));
    } catch (Exception e) {
        searchTime = UserCoreConstants.MAX_SEARCH_TIME;
    }

    if (maxItemLimit < 0 || maxItemLimit > givenMax) {
        maxItemLimit = givenMax;
    }

    ps.setMaxRows(maxItemLimit);
    try {
        ps.setQueryTimeout(searchTime);
    } catch (Exception e) {
        // this can be ignored since timeout method is not implemented
        log.debug(e);
    }
}

From source file:ro.nextreports.engine.queryexec.QueryExecutor.java

private PreparedStatement createStatement(String queryString) throws QueryException {
    // create the prepared statement
    PreparedStatement pstmt;
    try {//w  ww  .  ja v  a  2s  .c o  m

        boolean hasScrollType = false;
        try {
            hasScrollType = DialectUtil.isSupportedResultSetType(conn, ResultSet.TYPE_SCROLL_INSENSITIVE);
        } catch (Exception ex) {
            ex.printStackTrace();
            LOG.error(ex.getMessage(), ex);
        }
        int resultSetType = hasScrollType ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY;

        if (QueryUtil.isProcedureCall(queryString)) {
            pstmt = conn.prepareCall("{" + queryString + "}", resultSetType, ResultSet.CONCUR_READ_ONLY);
        } else {
            if (isCsv) {
                pstmt = conn.prepareStatement(queryString);
            } else {
                boolean keepCursorsOverCommit = false;
                try {
                    Dialect dialect = DialectUtil.getDialect(conn);
                    keepCursorsOverCommit = dialect.needsHoldCursorsForPreparedStatement();
                } catch (DialectException e) {
                    e.printStackTrace();
                    LOG.error(e.getMessage(), e);
                }
                if (keepCursorsOverCommit) {
                    pstmt = conn.prepareStatement(queryString, resultSetType, ResultSet.CONCUR_READ_ONLY,
                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
                } else {
                    pstmt = conn.prepareStatement(queryString, resultSetType, ResultSet.CONCUR_READ_ONLY);
                }
            }
        }
        // ignore queryTimeout and maxRows (some drivers - derby - not implement
        // these feature yet)
        try {
            // set timeout
            pstmt.setQueryTimeout(timeout);

            // set max rows
            pstmt.setMaxRows(maxRows);
        } catch (SQLException e) {
            LOG.warn(e);
        }
    } catch (SQLException e) {
        throw new QueryException(e);
    }

    return pstmt;
}