/** low-level test for critical performance issue described in bug [3078236] */ public void testExceptionChaining() throws SQLException { for (boolean warn : new boolean[] {false, true}) { long base = 0; for (int size : new int[] {10, 25, 50, 100, 500}) { SQLDiagnostic diag = new SQLDiagnostic(0); System.gc(); long start = System.nanoTime(); for (int c = 0; c < size * 1000; c++) { if (warn) { diag.addWarning(new SQLWarning()); } else { diag.addException(new SQLException()); } } long avg = (System.nanoTime() - start) / size; // nanoseconds per 1000 exceptions // ensure time grows linear (allows factor 2 deviation, but not exponential effects) assertTrue( "chaining " + size * 1000 + (warn ? " warnings" : " exceptions") + " slowed down too much (" + base / 1000000 + " ms -> " + avg / 1000000 + " ms per 1000)", base == 0 || avg < base * 2); base = base == 0 ? avg : base; } } }
protected Connection getConnection() throws Exception { long currTime = System.nanoTime(); if (currTime - connLastUsed > CONN_TIME_OUT) { synchronized (this) { Connection tmpConn = factory.call(); closeConnection(); connLastUsed = System.nanoTime(); return conn = tmpConn; } } else { connLastUsed = currTime; return conn; } }
/** * Save an MVD to a file * * @param mvd the MVD to save * @param dst the file to save it to * @param rb database properties file * @param folderId id of the folder to contain it in * @throws Exception raised if an error occurred */ public static void externalise(MVD mvd, File dst, int folderId, Properties rb) throws Exception { long start = System.nanoTime() / 1000; System.gc(); long startMem = Runtime.getRuntime().freeMemory(); int size = mvd.dataSize(); byte[] data = new byte[size]; int nBytes = mvd.serialise(data); assert nBytes == size : "MVD shorter than predicted"; String str = Base64.encodeBytes(data, Base64.GZIP); if (rb == null) writeToFile(dst, str); else writeToDatabase(dst.getName(), str, mvd.description, folderId, rb); long end = System.nanoTime() / 1000; long endMem = Runtime.getRuntime().freeMemory(); System.out.println("internalise took " + (end - start) + " microseconds"); System.out.println("Memory used " + (startMem - endMem) + " bytes"); }
public ResultSetIterator(String query) { final List<String> colNames; try { Connection c = getConnection(); stmt = createStatement(c); LOG.debug("Executing SQL: " + query); long start = System.nanoTime(); resultSet = executeStatement(stmt, query); LOG.trace( "Time taken for sql :" + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); colNames = readFieldNames(resultSet.getMetaData()); } catch (Exception e) { wrapAndThrow(SEVERE, e, "Unable to execute query: " + query); return; } if (resultSet == null) { rSetIterator = new ArrayList<Map<String, Object>>().iterator(); return; } rSetIterator = createIterator(stmt, resultSet, convertType, colNames, fieldNameVsType); }
private List<EventBean> execute( PreparedStatement preparedStatement, Object[] lookupValuePerStream) { if (ExecutionPathDebugLog.isDebugEnabled && log.isInfoEnabled()) { log.info(".execute Executing prepared statement '" + preparedStatementText + "'"); } // set parameters SQLInputParameterContext inputParameterContext = null; if (columnTypeConversionHook != null) { inputParameterContext = new SQLInputParameterContext(); } int count = 1; for (int i = 0; i < lookupValuePerStream.length; i++) { try { Object parameter = lookupValuePerStream[i]; if (ExecutionPathDebugLog.isDebugEnabled && log.isInfoEnabled()) { log.info( ".execute Setting parameter " + count + " to " + parameter + " typed " + ((parameter == null) ? "null" : parameter.getClass())); } if (columnTypeConversionHook != null) { inputParameterContext.setParameterNumber(i + 1); inputParameterContext.setParameterValue(parameter); parameter = columnTypeConversionHook.getParameterValue(inputParameterContext); } setObject(preparedStatement, count, parameter); } catch (SQLException ex) { throw new EPException("Error setting parameter " + count, ex); } count++; } // execute ResultSet resultSet; if (enableJDBCLogging && jdbcPerfLog.isInfoEnabled()) { long startTimeNS = System.nanoTime(); long startTimeMS = System.currentTimeMillis(); try { resultSet = preparedStatement.executeQuery(); } catch (SQLException ex) { throw new EPException("Error executing statement '" + preparedStatementText + '\'', ex); } long endTimeNS = System.nanoTime(); long endTimeMS = System.currentTimeMillis(); jdbcPerfLog.info( "Statement '" + preparedStatementText + "' delta nanosec " + (endTimeNS - startTimeNS) + " delta msec " + (endTimeMS - startTimeMS)); } else { try { resultSet = preparedStatement.executeQuery(); } catch (SQLException ex) { throw new EPException("Error executing statement '" + preparedStatementText + '\'', ex); } } // generate events for result set List<EventBean> rows = new LinkedList<EventBean>(); try { SQLColumnValueContext valueContext = null; if (columnTypeConversionHook != null) { valueContext = new SQLColumnValueContext(); } SQLOutputRowValueContext rowContext = null; if (outputRowConversionHook != null) { rowContext = new SQLOutputRowValueContext(); } int rowNum = 0; while (resultSet.next()) { int colNum = 1; Map<String, Object> row = new HashMap<String, Object>(); for (Map.Entry<String, DBOutputTypeDesc> entry : outputTypes.entrySet()) { String columnName = entry.getKey(); Object value; DatabaseTypeBinding binding = entry.getValue().getOptionalBinding(); if (binding != null) { value = binding.getValue(resultSet, columnName); } else { value = resultSet.getObject(columnName); } if (columnTypeConversionHook != null) { valueContext.setColumnName(columnName); valueContext.setColumnNumber(colNum); valueContext.setColumnValue(value); valueContext.setResultSet(resultSet); value = columnTypeConversionHook.getColumnValue(valueContext); } row.put(columnName, value); colNum++; } EventBean eventBeanRow = null; if (this.outputRowConversionHook == null) { eventBeanRow = eventAdapterService.adapterForTypedMap(row, eventType); } else { rowContext.setValues(row); rowContext.setRowNum(rowNum); rowContext.setResultSet(resultSet); Object rowData = outputRowConversionHook.getOutputRow(rowContext); if (rowData != null) { eventBeanRow = eventAdapterService.adapterForTypedBean(rowData, (BeanEventType) eventType); } } if (eventBeanRow != null) { rows.add(eventBeanRow); rowNum++; } } } catch (SQLException ex) { throw new EPException( "Error reading results for statement '" + preparedStatementText + '\'', ex); } if (enableJDBCLogging && jdbcPerfLog.isInfoEnabled()) { jdbcPerfLog.info("Statement '" + preparedStatementText + "' " + rows.size() + " rows"); } try { resultSet.close(); } catch (SQLException ex) { throw new EPException("Error closing statement '" + preparedStatementText + '\'', ex); } return rows; }
public List<Table> queryFields(DBConfig config) { List<Table> tableInfoList = new ArrayList<Table>(); Connection conn = null; try { List<TableConfiguration> tableList = (List<TableConfiguration>) config.getTableList(); if (tableList == null || tableList.size() <= 0) return tableInfoList; conn = ConnectionFactory.getInstance().getConnection(config); DatabaseMetaData databaseMetaData = conn.getMetaData(); for (TableConfiguration table : tableList) { Table tableInfo = new Table(); String localCatalog = table.getCatalog(); String localSchema = table.getSchema(); String localTableName = table.getTableName(); if (databaseMetaData.storesLowerCaseIdentifiers()) { localCatalog = localCatalog == null ? null : localCatalog.toLowerCase(); localSchema = localSchema == null ? null : localSchema.toLowerCase(); localTableName = localTableName == null ? null : localTableName.toLowerCase(); } else if (databaseMetaData.storesUpperCaseIdentifiers()) { localCatalog = localCatalog == null ? null : localCatalog.toUpperCase(); localSchema = localSchema == null ? null : localSchema.toUpperCase(); localTableName = localTableName == null ? null : localTableName.toUpperCase(); } Statement stmt = conn.createStatement(); ResultSet tableRs = stmt.executeQuery("SHOW CREATE TABLE " + localTableName); if (tableRs != null && tableRs.next()) { String create = tableRs.getString(2); String comment = parse(create); tableInfo.setComment(comment); } ResultSet rs = databaseMetaData.getColumns(localCatalog, localSchema, localTableName, null); tableInfo.setSerialVersionUID(System.nanoTime() + "L"); while (rs.next()) { tableInfo.setCatalog(rs.getString("TABLE_CAT")); tableInfo.setSchema(rs.getString("TABLE_SCHEM")); tableInfo.setName(rs.getString("TABLE_NAME")); tableInfo.setCode(rs.getString("TABLE_NAME")); Column introspectedColumn = new Column(); introspectedColumn.setTableAlias(table.getTableName()); introspectedColumn.setName(rs.getString("COLUMN_NAME")); introspectedColumn.setJdbcType(rs.getInt("DATA_TYPE")); introspectedColumn.setDataType( JdbcTypeNameTranslator.getJdbcTypeName(rs.getInt("DATA_TYPE"))); // $NON-NLS-1$ introspectedColumn.setLength(rs.getInt("COLUMN_SIZE")); // $NON-NLS-1$ introspectedColumn.setCode(rs.getString("COLUMN_NAME")); /*introspectedColumn.setActualColumnName(rs .getString("COLUMN_NAME"));*/ //$NON-NLS-1$ introspectedColumn.setNullable( rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable); // $NON-NLS-1$ introspectedColumn.setScale(rs.getInt("DECIMAL_DIGITS")); // $NON-NLS-1$ introspectedColumn.setComment(rs.getString("REMARKS")); tableInfo.addColumn(introspectedColumn); PropertyBean pb = new PropertyBean(); pb.setName(convertFirstUpper(getFieldName(rs.getString("COLUMN_NAME")))); pb.setType(JdbcType2Java.calculateJavaType(introspectedColumn)); String importType = JdbcType2Java.importJavaType(introspectedColumn); if (importType != null && !importType.equals("")) { if (importType.indexOf("java.lang") < 0 && !tableInfo.getImportList().contains(importType)) tableInfo.getImportList().add(importType); } tableInfo.getPropertyBeanList().add(pb); } closeResultSet(rs); rs = databaseMetaData.getPrimaryKeys(localCatalog, localSchema, localTableName); while (rs.next()) { tableInfo.addPrimaryKeyColumn(rs.getString("COLUMN_NAME")); } closeResultSet(rs); tableInfoList.add(tableInfo); } } catch (Exception e) { e.printStackTrace(); } return tableInfoList; }
/** Executes the current statement, and handles any SQLException. */ public void execute() { assert state == State.FRESH : "cannot re-execute"; state = State.ACTIVE; String status = "failed"; Statement statement = null; try { this.jdbcConnection = dataSource.getConnection(); querySemaphore.enter(); haveSemaphore = true; // Trace start of execution. if (RolapUtil.SQL_LOGGER.isDebugEnabled()) { StringBuilder sqllog = new StringBuilder(); sqllog.append(id).append(": ").append(locus.component).append(": executing sql ["); if (sql.indexOf('\n') >= 0) { // SQL appears to be formatted as multiple lines. Make it // start on its own line. sqllog.append("\n"); } sqllog.append(sql); sqllog.append(']'); RolapUtil.SQL_LOGGER.debug(sqllog.toString()); } // Execute hook. RolapUtil.ExecuteQueryHook hook = RolapUtil.getHook(); if (hook != null) { hook.onExecuteQuery(sql); } startTimeNanos = System.nanoTime(); startTimeMillis = System.currentTimeMillis(); if (resultSetType < 0 || resultSetConcurrency < 0) { statement = jdbcConnection.createStatement(); } else { statement = jdbcConnection.createStatement(resultSetType, resultSetConcurrency); } if (maxRows > 0) { statement.setMaxRows(maxRows); } // First make sure to register with the execution instance. locus.execution.registerStatement(locus, statement); locus .getServer() .getMonitor() .sendEvent( new SqlStatementStartEvent( startTimeMillis, id, locus, sql, getPurpose(), getCellRequestCount())); this.resultSet = statement.executeQuery(sql); // skip to first row specified in request this.state = State.ACTIVE; if (firstRowOrdinal > 0) { if (resultSetType == ResultSet.TYPE_FORWARD_ONLY) { for (int i = 0; i < firstRowOrdinal; ++i) { if (!this.resultSet.next()) { this.state = State.DONE; break; } } } else { if (!this.resultSet.absolute(firstRowOrdinal)) { this.state = State.DONE; } } } long timeMillis = System.currentTimeMillis(); long timeNanos = System.nanoTime(); final long executeNanos = timeNanos - startTimeNanos; final long executeMillis = executeNanos / 1000000; Util.addDatabaseTime(executeMillis); status = ", exec " + executeMillis + " ms"; locus .getServer() .getMonitor() .sendEvent( new SqlStatementExecuteEvent(timeMillis, id, locus, sql, getPurpose(), executeNanos)); // Compute accessors. They ensure that we use the most efficient // method (e.g. getInt, getDouble, getObject) for the type of the // column. Even if you are going to box the result into an object, // it is better to use getInt than getObject; the latter might // return something daft like a BigDecimal (does, on the Oracle JDBC // driver). accessors.clear(); for (Type type : guessTypes()) { accessors.add(createAccessor(accessors.size(), type)); } } catch (Throwable e) { status = ", failed (" + e + ")"; Util.close(resultSet, statement, jdbcConnection); if (haveSemaphore) { haveSemaphore = false; querySemaphore.leave(); } if (e instanceof Error) { throw (Error) e; } else { throw handle(e); } } finally { RolapUtil.SQL_LOGGER.debug(id + ": " + status); if (RolapUtil.LOGGER.isDebugEnabled()) { RolapUtil.LOGGER.debug(locus.component + ": executing sql [" + sql + "]" + status); } } }