use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class UserSessionStore method getActiveSessionCount.
/**
* Counts the number of active sessions of the given tenant domain. For a session to be active, the last access
* time of the session should not be earlier than the session timeout time.
*
* @param tenantDomain tenant domain
* @return number of active sessions of the given tenant domain
* @throws UserSessionException if something goes wrong
*/
public int getActiveSessionCount(String tenantDomain) throws UserSessionException {
int activeSessionCount = 0;
int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
long idleSessionTimeOut = TimeUnit.SECONDS.toMillis(IdPManagementUtil.getIdleSessionTimeOut(tenantDomain));
long currentTime = System.currentTimeMillis();
long minTimestamp = currentTime - idleSessionTimeOut;
try (Connection connection = IdentityDatabaseUtil.getSessionDBConnection(false)) {
String sqlStmt = isH2DB() ? SQLQueries.SQL_GET_ACTIVE_SESSION_COUNT_BY_TENANT_H2 : SQLQueries.SQL_GET_ACTIVE_SESSION_COUNT_BY_TENANT;
try (PreparedStatement preparedStatement = connection.prepareStatement(sqlStmt)) {
preparedStatement.setString(1, SessionMgtConstants.LAST_ACCESS_TIME);
preparedStatement.setString(2, String.valueOf(minTimestamp));
preparedStatement.setString(3, String.valueOf(currentTime));
preparedStatement.setInt(4, tenantId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
activeSessionCount = resultSet.getInt(1);
}
}
IdentityDatabaseUtil.commitTransaction(connection);
}
} catch (DataAccessException | SQLException e) {
throw new UserSessionException("Error while retrieving active session count of the tenant domain, " + tenantDomain, e);
}
return activeSessionCount;
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationManagerTest method removeCreatedTimeColumn.
private void removeCreatedTimeColumn() throws DataAccessException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
jdbcTemplate.executeUpdate(REMOVE_CREATED_TIME_COLUMN_H2);
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class IdPManagementDAO method getIdPNameByMetadataProperty.
/**
* Retrieves the first matching IDP for the given metadata property.
* Intended to ony be used to retrieve IDP name based on a unique metadata property.
*
* @param dbConnection Optional. DB connection.
* @param property IDP metadata property name.
* @param value Value associated with given Property.
* @param tenantId Tenant id whose information is requested.
* @return Identity Provider name.
* @throws IdentityProviderManagementException IdentityProviderManagementException.
*/
public String getIdPNameByMetadataProperty(Connection dbConnection, String property, String value, int tenantId) throws IdentityProviderManagementException {
PreparedStatement prepStmt = null;
ResultSet rs = null;
boolean dbConnectionInitialized = true;
if (dbConnection == null) {
dbConnection = IdentityDatabaseUtil.getDBConnection(false);
} else {
dbConnectionInitialized = false;
}
try {
String sqlStmt = isH2DB() ? IdPManagementConstants.SQLQueries.GET_IDP_NAME_BY_METADATA_H2 : IdPManagementConstants.SQLQueries.GET_IDP_NAME_BY_METADATA;
prepStmt = dbConnection.prepareStatement(sqlStmt);
prepStmt.setString(1, property);
prepStmt.setString(2, value);
prepStmt.setInt(3, tenantId);
rs = prepStmt.executeQuery();
String idPName = null;
if (rs.next()) {
idPName = rs.getString(1);
}
return idPName;
} catch (DataAccessException | SQLException e) {
throw new IdentityProviderManagementException("Error occurred while retrieving Identity Provider " + "information for IDP metadata property name: " + property + " value: " + value, e);
} finally {
if (dbConnectionInitialized) {
IdentityDatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
} else {
IdentityDatabaseUtil.closeAllConnections(null, rs, prepStmt);
}
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class SecretDAOImpl method isExistingSecret.
@Override
public boolean isExistingSecret(String secretId, int tenantId) throws SecretManagementException {
NamedJdbcTemplate jdbcTemplate = getNewTemplate();
String secretName;
try {
secretName = jdbcTemplate.fetchSingleRecord(GET_SECRET_NAME_BY_ID, (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_SECRET_NAME), preparedStatement -> {
preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ID, secretId);
preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantId);
});
return StringUtils.isNotEmpty(secretName);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_GET_SECRET, "id = " + secretId, e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class SecretDAOImpl method getSecretTypeByName.
@Override
public SecretType getSecretTypeByName(String secretTypeName) throws SecretManagementException {
NamedJdbcTemplate jdbcTemplate = getNewTemplate();
SecretType secretTypeResponse;
try {
secretTypeResponse = jdbcTemplate.fetchSingleRecord(GET_SECRET_TYPE_BY_NAME, (resultSet, rowNumber) -> {
SecretType secretType = new SecretType();
secretType.setId(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID));
secretType.setName(resultSet.getString(DB_SCHEMA_COLUMN_NAME_NAME));
secretType.setDescription(resultSet.getString(DB_SCHEMA_COLUMN_NAME_DESCRIPTION));
return secretType;
}, preparedStatement -> preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_NAME, secretTypeName));
return secretTypeResponse;
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_RETRIEVE_SECRET_TYPE, secretTypeName, e);
}
}
Aggregations