Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing sonar bugs of severity blocker from module Batch Connector PAYARA-3133 #3172

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY;

/**
*
*
* DB2 Persistence Manager
*/

Expand All @@ -72,7 +72,7 @@ public class DB2PersistenceManager extends JBatchJDBCPersistenceManager implemen
private final static Logger logger = Logger.getLogger(CLASSNAME);

private IBatchConfig batchConfig = null;

// db2 create table strings
protected Map<String, String> createDB2Strings;

Expand All @@ -87,7 +87,7 @@ public void init(IBatchConfig batchConfig)
jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName();
prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, "");
suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, "");

try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(jndiName);
Expand Down Expand Up @@ -119,7 +119,7 @@ public void init(IBatchConfig batchConfig)
"JNDI name is not defined.");
}


try {
if (!isSchemaValid()) {
setDefaultSchema();
Expand All @@ -133,7 +133,7 @@ public void init(IBatchConfig batchConfig)

logger.config("Exiting CLASSNAME.init()");
}

/**
* Check the schema exists
* @return
Expand All @@ -143,43 +143,37 @@ public void init(IBatchConfig batchConfig)
protected boolean isSchemaValid() throws SQLException {

boolean result = false;
Connection conn = null;
DatabaseMetaData dbmd = null;
ResultSet rs = null;

logger.entering(CLASSNAME, "isDB2SchemaValid");
try {
conn = getConnectionToDefaultSchema();
dbmd = conn.getMetaData();
rs = dbmd.getSchemas();

while (rs.next()) {

String schemaname = rs.getString("TABLE_SCHEM");
if (schema.equalsIgnoreCase(schemaname)) {
logger.exiting(CLASSNAME, "isSchemaValid", true);
return true;
try (Connection conn = getConnectionToDefaultSchema()) {
DatabaseMetaData dbmd = conn.getMetaData();
try (ResultSet rs = dbmd.getSchemas()) {
while (rs.next()) {
String schemaname = rs.getString("TABLE_SCHEM");
if (schema.equalsIgnoreCase(schemaname)) {
logger.exiting(CLASSNAME, "isSchemaValid", true);
return true;
}
}
}

} catch (SQLException e) {
logger.severe(e.getLocalizedMessage());
throw e;
} finally {
cleanupConnection(conn, rs, null);
}
logger.exiting(CLASSNAME, "isDB2SchemaValid", false);

return result;

}

/**
* Check the relevant db2 tables exist in the relevant schema
* @throws SQLException
*/
@Override
protected void checkTables() throws SQLException {

logger.entering(CLASSNAME, "checkDB2Tables");
setCreateDB2StringsMap(tableNames);
createTableIfNotExists(tableNames.get(CHECKPOINT_TABLE_KEY),
Expand All @@ -206,8 +200,6 @@ protected void checkTables() throws SQLException {

@Override
public boolean checkIfTableExists(DataSource dSource, String tableName, String schemaName) {
Statement statement = null;
ResultSet resultSet = null;
dataSource = dSource;

boolean result = true;
Expand All @@ -219,26 +211,30 @@ public boolean checkIfTableExists(DataSource dSource, String tableName, String s
setDefaultSchema();
}

statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String query = "select name from sysibm.systables where name ="
+ "\'" + tableName.toUpperCase() + "\'" + "and type = 'T'";
resultSet = statement.executeQuery(query);
try (Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY)) {

int rowcount = getTableRowCount(resultSet);
String query = "select name from sysibm.systables where name ="
+ "\'" + tableName.toUpperCase() + "\'" + "and type = 'T'";

if (rowcount == 0) {
if (!resultSet.next()) {
result = false;
}
}
try (ResultSet resultSet = statement.executeQuery(query)) {
int rowcount = getTableRowCount(resultSet);

if (rowcount == 0) {
if (!resultSet.next()) {
result = false;
}
}
}

}
} catch (SQLException ex) {
logger.severe(ex.getLocalizedMessage());
}

return result;
}

/**
* Method invoked to insert the DB2 create table strings into a hashmap
**/
Expand Down
Loading