use of java.sql.SQLTimeoutException in project cerberus-source by cerberustesting.
the class SQLService method executeUpdate.
@Override
public MessageEvent executeUpdate(String system, String country, String environment, String database, String sql) {
String connectionName;
CountryEnvironmentDatabase countryEnvironmentDatabase;
MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
try {
countryEnvironmentDatabase = this.countryEnvironmentDatabaseService.convert(this.countryEnvironmentDatabaseService.readByKey(system, country, environment, database));
if (countryEnvironmentDatabase != null) {
connectionName = countryEnvironmentDatabase.getConnectionPoolName();
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_GENERIC);
msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
if (!(StringUtil.isNullOrEmpty(connectionName))) {
if (connectionName.equals("cerberus" + System.getProperty("org.cerberus.environment"))) {
return new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_AGAINST_CERBERUS);
} else {
try (Connection connection = this.databaseSpring.connect(connectionName);
PreparedStatement preStat = connection.prepareStatement(sql)) {
Integer sqlTimeout = parameterService.getParameterIntegerByKey("cerberus_actionexecutesqlupdate_timeout", system, 60);
preStat.setQueryTimeout(sqlTimeout);
try {
LOG.info("Sending to external Database (executeUpdate) : '" + connectionName + "' SQL '" + sql + "'");
preStat.executeUpdate();
int nbUpdate = preStat.getUpdateCount();
msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_EXECUTESQLUPDATE).resolveDescription("NBROWS", String.valueOf(nbUpdate)).resolveDescription("JDBC", connectionName).resolveDescription("SQL", sql);
} catch (SQLTimeoutException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_TIMEOUT);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%TIMEOUT%", String.valueOf(sqlTimeout)));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
} catch (SQLException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
}
} catch (SQLException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
} catch (NullPointerException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_CANNOTACCESSJDBC);
msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
}
}
} else {
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASECONFIGUREDBUTJDBCPOOLEMPTY);
msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
msg.setDescription(msg.getDescription().replace("%ENV%", environment));
msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
}
} else {
msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASENOTCONFIGURED);
msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
msg.setDescription(msg.getDescription().replace("%ENV%", environment));
msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
}
} catch (CerberusException ex) {
LOG.error(ex.toString());
}
return msg;
}
use of java.sql.SQLTimeoutException in project cerberus-source by cerberustesting.
the class SQLService method queryDatabaseNColumns.
@Override
public AnswerList queryDatabaseNColumns(String connectionName, String sql, int rowLimit, int defaultTimeOut, String system, HashMap<String, String> columnsToGet) {
AnswerList listResult = new AnswerList();
List<HashMap<String, String>> list;
int maxSecurityFetch = parameterService.getParameterIntegerByKey("cerberus_testdatalib_fetchmax", system, 100);
int maxFetch;
if (rowLimit > 0 && rowLimit < maxSecurityFetch) {
maxFetch = rowLimit;
} else {
maxFetch = maxSecurityFetch;
}
int nbFetch = 0;
int nbColMatch = 0;
String error_desc = "";
MessageEvent msg = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS);
msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
try (Connection connection = this.databaseSpring.connect(connectionName);
PreparedStatement preStat = connection.prepareStatement(sql)) {
preStat.setQueryTimeout(defaultTimeOut);
try {
LOG.info("Sending to external Database (queryDatabaseNColumns) : '" + connectionName + "' SQL '" + sql + "'");
ResultSet resultSet = preStat.executeQuery();
int nrColumns = resultSet.getMetaData().getColumnCount();
list = new ArrayList<HashMap<String, String>>();
try {
while ((resultSet.next()) && (nbFetch < maxFetch)) {
nbColMatch = 0;
HashMap<String, String> row = new HashMap<String, String>();
for (Map.Entry<String, String> entry : columnsToGet.entrySet()) {
String column = entry.getValue();
String name = entry.getKey();
try {
String valueSQL = resultSet.getString(column);
if (valueSQL == null) {
// If data is null from the database, we convert it to the static string <NULL>.
valueSQL = "<NULL>";
}
// We put the result of the subData.
row.put(name, valueSQL);
nbColMatch++;
} catch (SQLException exception) {
if (nbFetch == 0) {
if ("".equals(error_desc)) {
error_desc = column;
} else {
error_desc = error_desc + ", " + column;
}
}
}
}
list.add(row);
nbFetch++;
}
listResult.setDataList(list);
listResult.setTotalRows(list.size());
if (list.isEmpty()) {
// No data was fetched.
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_NODATA);
} else if (nbColMatch == 0) {
// None of the columns could be match.
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_NOCOLUMNMATCH);
msg.setDescription(msg.getDescription().replace("%BADCOLUMNS%", error_desc));
} else if (!("".equals(error_desc))) {
// At least a column could not be parsed
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_COLUMNNOTMATCHING);
msg.setDescription(msg.getDescription().replace("%BADCOLUMNS%", error_desc));
}
} catch (SQLTimeoutException exception) {
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_TIMEOUT);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%TIMEOUT%", String.valueOf(defaultTimeOut)));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
} finally {
if (resultSet != null) {
resultSet.close();
}
}
} catch (SQLTimeoutException exception) {
LOG.warn("TimeOut " + exception.toString());
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_TIMEOUT);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%TIMEOUT%", String.valueOf(defaultTimeOut)));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
} catch (SQLException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_ERROR);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
}
} catch (SQLException exception) {
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_ERROR);
msg.setDescription(msg.getDescription().replace("%SQL%", sql));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
} catch (NullPointerException exception) {
// TODO check where exception occur
LOG.warn(exception.toString());
msg = new MessageEvent(MessageEventEnum.PROPERTY_FAILED_SQL_CANNOTACCESSJDBC);
msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
}
listResult.setResultMessage(msg);
return listResult;
}
use of java.sql.SQLTimeoutException in project jaybird by FirebirdSQL.
the class WireDatabaseConnectionTest method testSocketTimeout_noResponse.
/**
* Tests the connect timeout if the server does not respond.
*/
@Test
public void testSocketTimeout_noResponse() throws Exception {
BlackholeServer server = new BlackholeServer();
Thread thread = new Thread(server);
thread.start();
long startTime = System.currentTimeMillis();
try {
connectionInfo.setPortNumber(server.getPort());
connectionInfo.setDatabaseName("somedb");
connectionInfo.setSoTimeout(2000);
WireDatabaseConnection gdsConnection = new WireDatabaseConnection(connectionInfo);
gdsConnection.socketConnect();
gdsConnection.identify();
fail("Expected connection to fail");
} catch (SQLTimeoutException e) {
long endTime = System.currentTimeMillis();
long difference = endTime - startTime;
assertEquals("Expected error code for \"Unable to complete network request\"", 335544721, e.getErrorCode());
assertEquals("Unexpected timeout duration (in ms)", 2000, difference, TIMEOUT_DELTA_MS);
} catch (SQLException e) {
e.printStackTrace();
fail("Expected SQLTimeoutException to be thrown");
} finally {
server.stop();
thread.join();
}
}
use of java.sql.SQLTimeoutException in project derby by apache.
the class LoginTimeoutTest method tryTimeout.
private void tryTimeout(Connector connector, boolean shouldSucceed) throws Exception {
long startTime = System.currentTimeMillis();
try {
Connection conn = connector.getConnection(RUTH, RUTH_PASSWORD);
println(" Got a " + conn.getClass().getName());
conn.close();
if (!shouldSucceed) {
// sometimes the connect succeeds, see DERBY-6250.
// adding more details to fail message.
long duration = System.currentTimeMillis() - startTime;
String message = "Should not have been able to connect! \n " + " connector: " + connector + " Experiment took " + duration + " milliseconds. \n " + " seconds sleep time was: " + SluggishAuthenticator.secondsToSleep;
fail(message);
}
} catch (SQLException se) {
if (shouldSucceed) {
fail("Should have been able to connect!", se);
}
assertTrue("Didn't expect to see a " + se.getClass().getName(), (se instanceof SQLTimeoutException));
assertSQLState(LOGIN_TIMEOUT, se);
}
long duration = System.currentTimeMillis() - startTime;
println(" Experiment took " + duration + " milliseconds.");
}
use of java.sql.SQLTimeoutException in project derby by apache.
the class StatementTest method test_jdbc4_1_queryTimeoutException.
/**
* Test that Statement.setQueryTimeout() causes statements to
* raise SQLTimeoutException per the JDBC 4.1 spec clarification.
*/
public void test_jdbc4_1_queryTimeoutException() throws Exception {
SQLException se = null;
PreparedStatement ps = prepareStatement("select columnnumber from sys.syscolumns c, sys.systables t\n" + "where t.tablename = 'SYSTABLES'\n" + "and t.tableid = c.referenceid\n" + "and mod( delay_st( 5, c.columnnumber ), 3 ) = 0");
println("Testing timeout exception for a " + ps.getClass().getName());
SetQueryTimeoutTest.StatementExecutor executor = new SetQueryTimeoutTest.StatementExecutor(ps, true, 1);
executor.start();
executor.join();
ps.close();
se = executor.getSQLException();
assertNotNull(se);
assertEquals(SQLTimeoutException.class.getName(), se.getClass().getName());
}
Aggregations