@Override @RequiredPermission(Permission.MANAGE_SETTINGS) @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public long vacuum(Subject whoami, String[] tableNames) { long duration = 0; Connection conn = null; DatabaseType dbtype = null; try { conn = dataSource.getConnection(); dbtype = DatabaseTypeFactory.getDatabaseType(conn); if (!DatabaseTypeFactory.isPostgres(dbtype)) { return -1; } if (tableNames == null) // no names given -> operate on all tables. { tableNames = new String[1]; tableNames[0] = null; } for (String tableName : tableNames) { duration += doCommand(dbtype, conn, SQL_VACUUM, tableName); } return duration; } catch (Exception e) { LOG.error("Error vacuuming database: " + e.getMessage(), e); return duration; } finally { if (dbtype != null) { dbtype.closeConnection(conn); } } }
/** * DO NOT OVERRIDE. * * <p>DO NOT DEFINE AN @BeforeMethod Instead, override {@link #beforeMethod()}. If you must * override, for example, if you need to use special attributes on your annotation, then ensure * you protect the code with and {@link #inContainer()} call. */ @BeforeMethod protected void __beforeMethod(Method method) throws Throwable { // Note that Arquillian calls the testng BeforeMethod twice (as of 1.0.2.Final, once // out of container and once in container. In general the expectation is to execute it // one time, and doing it in container allows for the expected injections and context. if (inContainer()) { try { // Make sure we set the db type for tests that may need it (normally done in StartupBean) if (null == DatabaseTypeFactory.getDefaultDatabaseType()) { Connection conn = null; try { conn = getConnection(); DatabaseTypeFactory.setDefaultDatabaseType(DatabaseTypeFactory.getDatabaseType(conn)); } catch (Exception e) { System.err.println( "!!! WARNING !!! cannot set default database type, some tests may fail"); e.printStackTrace(); } finally { if (null != conn) { conn.close(); } } } beforeMethod(); beforeMethod(method); } catch (Throwable t) { // Arquillian is eating these, make sure they show up in some way System.out.println("BEFORE METHOD FAILURE, TEST DID NOT RUN!!! [" + method.getName() + "]"); t.printStackTrace(); throw t; } } }
@Override @RequiredPermission(Permission.MANAGE_SETTINGS) @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public long reindex(Subject whoami) { Connection conn = null; DatabaseType dbtype = null; try { conn = dataSource.getConnection(); dbtype = DatabaseTypeFactory.getDatabaseType(conn); long duration = 0; if (DatabaseTypeFactory.isPostgres(dbtype)) { for (String table : TABLES_TO_REINDEX) { duration += doCommand(dbtype, conn, SQL_REINDEX, table); } } else if (DatabaseTypeFactory.isOracle(dbtype)) { for (String index : ORA_INDEXES_TO_REBUILD) { duration += doCommand(dbtype, conn, SQL_REBUILD, index); } } else { return -1; } return duration; } catch (Exception e) { LOG.error("Error reindexing database", e); throw new RuntimeException("Error reindexing database", e); } finally { if (dbtype != null) { dbtype.closeConnection(conn); } } }
@BeforeMethod public static void startTest() { if (DatabaseTypeFactory.getDefaultDatabaseType() == null) { try { Connection conn = getConnection(); DatabaseTypeFactory.setDefaultDatabaseType(DatabaseTypeFactory.getDatabaseType(conn)); } catch (Exception e) { System.err.println("!!! WARNING !!! cannot set default database type, some tests may fail"); e.printStackTrace(); } } if (stats != null) start = stats.getQueryExecutionCount(); else start = 0; }
private void initializeServer() { // Ensure the class is loaded and the dbType is set for our current db Connection conn = null; try { conn = dataSource.getConnection(); DatabaseTypeFactory.setDefaultDatabaseType(DatabaseTypeFactory.getDatabaseType(conn)); } catch (Exception e) { log.error("Could not initialize server.", e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { log.error("Failed to close temporary connection used for server initialization.", e); } } } // Ensure that this server is registered in the database. createDefaultServerIfNecessary(); // immediately put the server into MM if configured to do so if (ServerCommunicationsServiceUtil.getService().getMaintenanceModeAtStartup()) { log.info("Server is configured to start up in MAINTENANCE mode."); Server server = serverManager.getServer(); Integer[] serverId = new Integer[] {server.getId()}; topologyManager.updateServerMode( LookupUtil.getSubjectManager().getOverlord(), serverId, OperationMode.MAINTENANCE); } // Establish the current server mode for the server. This will move the server to NORMAL // mode from DOWN if necessary. This can also affect comm layer behavior. serverManager.establishCurrentServerMode(); if ("true".equals(System.getProperty("rhq.sync.endpoint-address", "false"))) { try { serverManager.syncEndpointAddress(); } catch (SyncEndpointAddressException e) { log.error("Failed to sync server endpoint address.", e); } } }
@Override @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public DatabaseType getDatabaseType() { Connection conn = null; DatabaseType dbtype = null; try { conn = dataSource.getConnection(); dbtype = DatabaseTypeFactory.getDatabaseType(conn); return dbtype; } catch (Exception e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { LOG.warn("Failed to close temporary connection", e); } } } }
@Override @RequiredPermission(Permission.MANAGE_SETTINGS) @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public long analyze(Subject whoami) { Connection conn = null; DatabaseType dbtype = null; try { conn = dataSource.getConnection(); dbtype = DatabaseTypeFactory.getDatabaseType(conn); if (!DatabaseTypeFactory.isPostgres(dbtype)) { return -1; } long duration = doCommand(dbtype, conn, SQL_ANALYZE, null); return duration; } catch (Exception e) { LOG.error("Error analyzing database", e); throw new RuntimeException("Error analyzing database", e); } finally { if (dbtype != null) { dbtype.closeConnection(conn); } } }
public boolean isPostgres() throws Exception { return DatabaseTypeFactory.getDatabaseType( getInitialContext(), RHQConstants.DATASOURCE_JNDI_NAME) instanceof PostgresqlDatabaseType; }
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // @TransactionTimeout( 60 * 60 ) public int _calculateAutoBaselinesINSERT(long amountOfData) throws Exception { long now = System.currentTimeMillis(); long computeTime = now; long endTime = now; long startTime = endTime - amountOfData; Connection conn = null; PreparedStatement insertQuery = null; try { // calculate the baselines for schedules that have no baseline yet (or were just deleted) // do everything via JDBC - our perf testing shows that we hit entity cache locking timeouts // when the entity manager performs native queries under heavy load conn = dataSource.getConnection(); DatabaseType dbType = DatabaseTypeFactory.getDatabaseType(conn); if (dbType instanceof PostgresqlDatabaseType || dbType instanceof H2DatabaseType) { insertQuery = conn.prepareStatement( MeasurementBaseline.NATIVE_QUERY_CALC_FIRST_AUTOBASELINE_POSTGRES); insertQuery.setLong(1, computeTime); insertQuery.setLong(2, startTime); insertQuery.setLong(3, endTime); insertQuery.setLong(4, startTime); } else if (dbType instanceof OracleDatabaseType) { insertQuery = conn.prepareStatement(MeasurementBaseline.NATIVE_QUERY_CALC_FIRST_AUTOBASELINE_ORACLE); insertQuery.setLong(1, computeTime); insertQuery.setLong(2, startTime); insertQuery.setLong(3, endTime); insertQuery.setLong(4, startTime); } else if (dbType instanceof SQLServerDatabaseType) { insertQuery = conn.prepareStatement( MeasurementBaseline.NATIVE_QUERY_CALC_FIRST_AUTOBASELINE_SQLSERVER); insertQuery.setLong(1, computeTime); insertQuery.setLong(2, startTime); insertQuery.setLong(3, endTime); insertQuery.setLong(4, startTime); } else { throw new IllegalArgumentException("Unknown database type, can't continue: " + dbType); } int inserted = insertQuery.executeUpdate(); return inserted; } finally { if (insertQuery != null) { try { insertQuery.close(); } catch (Exception e) { } } if (conn != null) { try { conn.close(); } catch (Exception e) { } } } }
/* * internal method, do not expose to the remote API */ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void insertCallTimeDataValues(Set<CallTimeData> callTimeDataSet) { int[] results; String insertValueSql; PreparedStatement ps = null; Connection conn = null; try { conn = rhqDs.getConnection(); DatabaseType dbType = DatabaseTypeFactory.getDatabaseType(conn); if (dbType instanceof Postgresql83DatabaseType) { Statement st = null; try { // Take advantage of async commit here st = conn.createStatement(); st.execute("SET synchronous_commit = off"); } finally { JDBCUtil.safeClose(st); } } if (dbType instanceof PostgresqlDatabaseType || dbType instanceof OracleDatabaseType || dbType instanceof H2DatabaseType) { String valueNextvalSql = JDBCUtil.getNextValSql(conn, "RHQ_calltime_data_value"); insertValueSql = String.format(CALLTIME_VALUE_INSERT_STATEMENT, valueNextvalSql); } else if (dbType instanceof SQLServerDatabaseType) { insertValueSql = CALLTIME_VALUE_INSERT_STATEMENT_AUTOINC; } else { throw new IllegalArgumentException("Unknown database type, can't continue: " + dbType); } ps = conn.prepareStatement(insertValueSql); for (CallTimeData callTimeData : callTimeDataSet) { ps.setInt(7, callTimeData.getScheduleId()); Set<String> callDestinations = callTimeData.getValues().keySet(); for (String callDestination : callDestinations) { CallTimeDataValue callTimeDataValue = callTimeData.getValues().get(callDestination); ps.setLong(1, callTimeDataValue.getBeginTime()); ps.setLong(2, callTimeDataValue.getEndTime()); ps.setDouble(3, callTimeDataValue.getMinimum()); ps.setDouble(4, callTimeDataValue.getMaximum()); ps.setDouble(5, callTimeDataValue.getTotal()); ps.setLong(6, callTimeDataValue.getCount()); ps.setString(8, callDestination); ps.addBatch(); } } results = ps.executeBatch(); int insertedRowCount = 0; for (int i = 0; i < results.length; i++) { if ((results[i] != 1) && (results[i] != -2)) // Oracle likes to return -2 becuase it doesn't track batch update counts { throw new MeasurementStorageException( "Failed to insert call-time data value rows - result [" + results[i] + "] for batch command [" + i + "] does not equal 1."); } insertedRowCount += results[i] == -2 ? 1 : results[i]; // If Oracle returns -2, just count 1 row; } notifyAlertConditionCacheManager( "insertCallTimeDataValues", callTimeDataSet.toArray(new CallTimeData[callTimeDataSet.size()])); if (insertedRowCount > 0) { MeasurementMonitor.getMBean().incrementCalltimeValuesInserted(insertedRowCount); log.debug("Inserted " + insertedRowCount + " call-time data value rows."); } } catch (SQLException e) { logSQLException("Failed to persist call-time data values", e); } catch (Throwable t) { log.error("Failed to persist call-time data values", t); } finally { JDBCUtil.safeClose(conn, ps, null); } }
/* * internal method, do not expose to the remote API */ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void insertCallTimeDataKeys(Set<CallTimeData> callTimeDataSet) { int[] results; String insertKeySql; PreparedStatement ps = null; Connection conn = null; try { conn = rhqDs.getConnection(); DatabaseType dbType = DatabaseTypeFactory.getDatabaseType(conn); if (dbType instanceof Postgresql83DatabaseType) { Statement st = null; try { // Take advantage of async commit here st = conn.createStatement(); st.execute("SET synchronous_commit = off"); } finally { JDBCUtil.safeClose(st); } } if (dbType instanceof PostgresqlDatabaseType || dbType instanceof OracleDatabaseType || dbType instanceof H2DatabaseType) { String keyNextvalSql = JDBCUtil.getNextValSql(conn, "RHQ_calltime_data_key"); insertKeySql = String.format(CALLTIME_KEY_INSERT_STATEMENT, keyNextvalSql); } else if (dbType instanceof SQLServerDatabaseType) { insertKeySql = CALLTIME_KEY_INSERT_STATEMENT_AUTOINC; } else { throw new IllegalArgumentException("Unknown database type, can't continue: " + dbType); } ps = conn.prepareStatement(insertKeySql); for (CallTimeData callTimeData : callTimeDataSet) { ps.setInt(1, callTimeData.getScheduleId()); ps.setInt(3, callTimeData.getScheduleId()); Set<String> callDestinations = callTimeData.getValues().keySet(); for (String callDestination : callDestinations) { ps.setString(2, callDestination); ps.setString(4, callDestination); ps.addBatch(); } } results = ps.executeBatch(); int insertedRowCount = 0; for (int i = 0; i < results.length; i++) { if (((results[i] < 0) || (results[i] > 1)) && (results[i] != -2)) // oracle returns -2 because it can't count updated rows { throw new MeasurementStorageException( "Failed to insert call-time data key rows - result [" + results[i] + "] for batch command [" + i + "] is less than 0 or greater than 1."); } insertedRowCount += results[i] == -2 ? 1 : results[i]; // If Oracle returns -2, just count 1 row } log.debug( "Inserted new call-time data key rows for " + ((insertedRowCount >= 0) ? insertedRowCount : "?") + " out of " + results.length + " reported key-value pairs."); } catch (SQLException e) { logSQLException("Failed to persist call-time data keys", e); } catch (Throwable t) { log.error("Failed to persist call-time data keys", t); } finally { JDBCUtil.safeClose(conn, ps, null); } }