use of java.sql.DatabaseMetaData in project opennms by OpenNMS.
the class JdbcStoredProcedureDetectorIT method setUp.
@Before
public void setUp() throws SQLException {
MockLogAppender.setupLogging();
m_detector = m_detectorFactory.createDetector();
String createSchema = "CREATE SCHEMA test";
String createProcedure = "CREATE FUNCTION test.isRunning () RETURNS bit AS 'BEGIN RETURN 1; END;' LANGUAGE 'plpgsql';";
String url = null;
String username = null;
Connection conn = null;
try {
conn = m_dataSource.getConnection();
DatabaseMetaData metaData = conn.getMetaData();
url = metaData.getURL();
username = metaData.getUserName();
Statement createStmt = conn.createStatement();
createStmt.executeUpdate(createSchema);
createStmt.close();
Statement stmt = conn.createStatement();
stmt.executeUpdate(createProcedure);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
if (conn != null) {
conn.close();
}
}
m_detector.setDbDriver("org.postgresql.Driver");
m_detector.setPort(5432);
m_detector.setUrl(url);
m_detector.setUser(username);
m_detector.setPassword("");
m_detector.setStoredProcedure("isRunning");
}
use of java.sql.DatabaseMetaData in project Activiti by Activiti.
the class TableDataManager method getTableMetaData.
public TableMetaData getTableMetaData(String tableName) {
TableMetaData result = new TableMetaData();
try {
result.setTableName(tableName);
DatabaseMetaData metaData = getDbSqlSession().getSqlSession().getConnection().getMetaData();
if ("postgres".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
tableName = tableName.toLowerCase();
}
String catalog = null;
if (getProcessEngineConfiguration().getDatabaseCatalog() != null && getProcessEngineConfiguration().getDatabaseCatalog().length() > 0) {
catalog = getProcessEngineConfiguration().getDatabaseCatalog();
}
String schema = null;
if (getProcessEngineConfiguration().getDatabaseSchema() != null && getProcessEngineConfiguration().getDatabaseSchema().length() > 0) {
if ("oracle".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
schema = getProcessEngineConfiguration().getDatabaseSchema().toUpperCase();
} else {
schema = getProcessEngineConfiguration().getDatabaseSchema();
}
}
ResultSet resultSet = metaData.getColumns(catalog, schema, tableName, null);
while (resultSet.next()) {
boolean wrongSchema = false;
if (schema != null && schema.length() > 0) {
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
String columnName = resultSet.getMetaData().getColumnName(i + 1);
if ("TABLE_SCHEM".equalsIgnoreCase(columnName) || "TABLE_SCHEMA".equalsIgnoreCase(columnName)) {
if (schema.equalsIgnoreCase(resultSet.getString(resultSet.getMetaData().getColumnName(i + 1))) == false) {
wrongSchema = true;
}
break;
}
}
}
if (wrongSchema == false) {
String name = resultSet.getString("COLUMN_NAME").toUpperCase();
String type = resultSet.getString("TYPE_NAME").toUpperCase();
result.addColumnMetaData(name, type);
}
}
} catch (SQLException e) {
throw new ActivitiException("Could not retrieve database metadata: " + e.getMessage());
}
if (result.getColumnNames().isEmpty()) {
// According to API, when a table doesn't exist, null should be returned
result = null;
}
return result;
}
use of java.sql.DatabaseMetaData in project Activiti by Activiti.
the class ProcessEngineConfigurationImpl method initDatabaseType.
public void initDatabaseType() {
Connection connection = null;
try {
connection = dataSource.getConnection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
String databaseProductName = databaseMetaData.getDatabaseProductName();
log.debug("database product name: '{}'", databaseProductName);
databaseType = databaseTypeMappings.getProperty(databaseProductName);
if (databaseType == null) {
throw new ActivitiException("couldn't deduct database type from database product name '" + databaseProductName + "'");
}
log.debug("using database type: {}", databaseType);
} catch (SQLException e) {
log.error("Exception while initializing Database connection", e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
log.error("Exception while closing the Database connection", e);
}
}
}
use of java.sql.DatabaseMetaData in project Activiti by Activiti.
the class DbSqlSession method executeSchemaResource.
private void executeSchemaResource(String operation, String component, String resourceName, InputStream inputStream) {
log.info("performing {} on {} with resource {}", operation, component, resourceName);
String sqlStatement = null;
String exceptionSqlStatement = null;
try {
Connection connection = sqlSession.getConnection();
Exception exception = null;
byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
String ddlStatements = new String(bytes);
String databaseType = dbSqlSessionFactory.getDatabaseType();
// Special DDL handling for certain databases
try {
if ("mysql".equals(databaseType)) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
int majorVersion = databaseMetaData.getDatabaseMajorVersion();
int minorVersion = databaseMetaData.getDatabaseMinorVersion();
log.info("Found MySQL: majorVersion=" + majorVersion + " minorVersion=" + minorVersion);
// Special care for MySQL < 5.6
if (majorVersion <= 5 && minorVersion < 6) {
ddlStatements = updateDdlForMySqlVersionLowerThan56(ddlStatements);
}
}
} catch (Exception e) {
log.info("Could not get database metadata", e);
}
BufferedReader reader = new BufferedReader(new StringReader(ddlStatements));
String line = readNextTrimmedLine(reader);
boolean inOraclePlsqlBlock = false;
while (line != null) {
if (line.startsWith("# ")) {
log.debug(line.substring(2));
} else if (line.startsWith("-- ")) {
log.debug(line.substring(3));
} else if (line.startsWith("execute java ")) {
String upgradestepClassName = line.substring(13).trim();
DbUpgradeStep dbUpgradeStep = null;
try {
dbUpgradeStep = (DbUpgradeStep) ReflectUtil.instantiate(upgradestepClassName);
} catch (ActivitiException e) {
throw new ActivitiException("database update java class '" + upgradestepClassName + "' can't be instantiated: " + e.getMessage(), e);
}
try {
log.debug("executing upgrade step java class {}", upgradestepClassName);
dbUpgradeStep.execute(this);
} catch (Exception e) {
throw new ActivitiException("error while executing database update java class '" + upgradestepClassName + "': " + e.getMessage(), e);
}
} else if (line.length() > 0) {
if ("oracle".equals(databaseType) && line.startsWith("begin")) {
inOraclePlsqlBlock = true;
sqlStatement = addSqlStatementPiece(sqlStatement, line);
} else if ((line.endsWith(";") && inOraclePlsqlBlock == false) || (line.startsWith("/") && inOraclePlsqlBlock == true)) {
if (inOraclePlsqlBlock) {
inOraclePlsqlBlock = false;
} else {
sqlStatement = addSqlStatementPiece(sqlStatement, line.substring(0, line.length() - 1));
}
Statement jdbcStatement = connection.createStatement();
try {
// no logging needed as the connection will log it
log.debug("SQL: {}", sqlStatement);
jdbcStatement.execute(sqlStatement);
jdbcStatement.close();
} catch (Exception e) {
if (exception == null) {
exception = e;
exceptionSqlStatement = sqlStatement;
}
log.error("problem during schema {}, statement {}", operation, sqlStatement, e);
} finally {
sqlStatement = null;
}
} else {
sqlStatement = addSqlStatementPiece(sqlStatement, line);
}
}
line = readNextTrimmedLine(reader);
}
if (exception != null) {
throw exception;
}
log.debug("activiti db schema {} for component {} successful", operation, component);
} catch (Exception e) {
throw new ActivitiException("couldn't " + operation + " db schema: " + exceptionSqlStatement, e);
}
}
use of java.sql.DatabaseMetaData in project opennms by OpenNMS.
the class JdbcCollector method isGroupAvailable.
// Simply check the database the query is supposed to connect to to see if it is available.
private static boolean isGroupAvailable(JdbcAgentState agentState, JdbcQuery query) {
LOG.debug("Checking availability of group {}", query.getQueryName());
boolean status = false;
ResultSet resultset = null;
Connection con = null;
try {
if (agentState.getUseDataSourceName()) {
initDatabaseConnectionFactory(agentState.getDataSourceName());
con = DataSourceFactory.getInstance(agentState.getDataSourceName()).getConnection();
} else {
con = agentState.getJdbcConnection();
}
DatabaseMetaData metadata = con.getMetaData();
resultset = metadata.getCatalogs();
while (resultset.next()) {
resultset.getString(1);
}
// The query worked, assume than the server is ok
if (resultset != null) {
status = true;
}
} catch (SQLException sqlEx) {
LOG.warn("Error checking group ({}) availability", query.getQueryName(), sqlEx);
agentState.setGroupIsAvailable(query.getQueryName(), status);
status = false;
} finally {
agentState.closeResultSet(resultset);
agentState.closeConnection(con);
}
LOG.debug("Group {} is {} available", query.getQueryName(), (status ? "" : "not"));
agentState.setGroupIsAvailable(query.getQueryName(), status);
return status;
}
Aggregations