@Override protected void cleanup(Context context) throws IOException, InterruptedException { try { if (conn != null) { conn.close(); } } catch (SQLException e) { throw new RuntimeException(e); } }
public static SQLException closeConnection(PhoenixConnection conn, SQLException sqlEx) { SQLException toReturn = sqlEx; try { conn.close(); } catch (SQLException e) { if (toReturn != null) { toReturn.setNextException(e); } else { toReturn = e; } } return toReturn; } @SuppressWarnings("deprecation")
protected static PhoenixConnection addMetaDataColumn(PhoenixConnection conn, long scn, String columnDef) throws SQLException { PhoenixConnection metaConnection = null; Statement stmt = null; try { metaConnection = new PhoenixConnection(conn, scn); try { stmt = metaConnection.createStatement(); stmt.executeUpdate("ALTER TABLE SYSTEM.\"TABLE\" ADD IF NOT EXISTS " + columnDef); return metaConnection; } finally { if(stmt != null) { stmt.close(); } } } finally { if(metaConnection != null) { metaConnection.close(); } } }
@Override public void close() throws SQLException { try { /* * Join the child mutation states in close, since this is called in a single threaded manner * after the parallel results have been processed. * If auto-commit is on for the cloned child connection, then the finalState here is an empty mutation * state (with no mutations). However, it still has the metrics for mutation work done by the * mutating-iterator. Joining the mutation state makes sure those metrics are passed over * to the parent connection. */ MutatingParallelIteratorFactory.this.connection.getMutationState().join(finalState); } finally { clonedConnection.close(); } }
if (connection != null) { try { connection.close(); } catch (SQLException e) { LOG.error("Error {} while closing connection in the PhoenixIndexMapper class ",
@Override public MutationState execute() throws SQLException { try { return client.createSchema(create); } finally { if (client.getConnection() != connection) { client.getConnection().close(); } } }
@Override public MutationState execute() throws SQLException { try { return client.createFunction(create); } finally { if (client.getConnection() != connection) { client.getConnection().close(); } } }
private static void mapChildViewsToNamespace(String connUrl, Properties props, List<TableInfo> viewInfoList) throws SQLException, SnapshotCreationException, IllegalArgumentException, IOException, InterruptedException { String tenantId = null; String prevTenantId = null; PhoenixConnection conn = null; for (TableInfo viewInfo : viewInfoList) { tenantId = viewInfo.getTenantId()!=null ? Bytes.toString(viewInfo.getTenantId()) : null; String viewName = SchemaUtil.getTableName(viewInfo.getSchemaName(), viewInfo.getTableName()); if (prevTenantId != tenantId) { if (tenantId != null) { props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId); } else { props.remove(PhoenixRuntime.TENANT_ID_ATTRIB); } if (conn!=null) conn.close(); conn = DriverManager.getConnection(connUrl, props).unwrap(PhoenixConnection.class); } logger.info(String.format("Upgrading view %s for tenantId %s..", viewName,tenantId)); UpgradeUtil.upgradeTable(conn, viewName); prevTenantId = tenantId; } }
} finally { try { oldMetaConnection.close(); } catch (SQLException e) { if (sqlE != null) {
@Override public MutationState execute() throws SQLException { try { return client.createTable(finalCreate, splits, parent, viewStatement, viewType, MetaDataUtil.getViewIndexIdDataType(), viewColumnConstants, isViewColumnReferenced); } finally { if (client.getConnection() != connection) { client.getConnection().close(); } } }
@Override public void run() { try { Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES); for (int i = 0; i < 50; i++) { Thread.sleep(20); synchronized (lock) { PhoenixConnection conn = null; try { conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class); conn.setAutoCommit(true); conn.createStatement().execute("DELETE FROM " + tableName); } finally { if (conn != null) conn.close(); } } } } catch (SQLException e) { throw new RuntimeException(e); } catch (InterruptedException e) { Thread.interrupted(); throw new RuntimeException(e); } finally { doneSignal.countDown(); } }
/** * Move child links form SYSTEM.CATALOG to SYSTEM.CHILD_LINK * @param oldMetaConnection caller should take care of closing the passed connection appropriately * @throws SQLException */ public static void moveChildLinks(PhoenixConnection oldMetaConnection) throws SQLException { PhoenixConnection metaConnection = null; try { // Need to use own connection with max time stamp to be able to read all data from SYSTEM.CATALOG metaConnection = new PhoenixConnection(oldMetaConnection, HConstants.LATEST_TIMESTAMP); logger.info("Upgrading metadata to add parent to child links for views"); metaConnection.commit(); String createChildLink = "UPSERT INTO SYSTEM.CHILD_LINK(TENANT_ID, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, COLUMN_FAMILY, LINK_TYPE) " + "SELECT TENANT_ID, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, COLUMN_FAMILY, LINK_TYPE " + "FROM SYSTEM.CATALOG " + "WHERE LINK_TYPE = 4"; metaConnection.createStatement().execute(createChildLink); metaConnection.commit(); String deleteChildLink = "DELETE FROM SYSTEM.CATALOG WHERE LINK_TYPE = 4 "; metaConnection.createStatement().execute(deleteChildLink); metaConnection.commit(); metaConnection.getQueryServices().clearCache(); } finally { if (metaConnection != null) { metaConnection.close(); } } }
@Override public void run() { try { Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES); int nRowsToUpsert = 1000; for (int i = 0; i < nRowsToUpsert; i++) { synchronized(lock) { PhoenixConnection conn = null; try { conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class); conn.createStatement().execute("UPSERT INTO " + tableName + " VALUES (" + (i % 10) + ", 0, 1)"); if ((i % 20) == 0 || i == nRowsToUpsert-1 ) { conn.commit(); } } finally { if (conn != null) conn.close(); } } } } catch (SQLException e) { throw new RuntimeException(e); } finally { doneSignal.countDown(); } }
private static void verifySequence(String tenantID, String sequenceName, String sequenceSchemaName, boolean exists, long value) throws SQLException { PhoenixConnection phxConn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class); String ddl = "SELECT " + PhoenixDatabaseMetaData.TENANT_ID + "," + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.SEQUENCE_NAME + "," + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE "; ddl += " TENANT_ID " + ((tenantID == null ) ? "IS NULL " : " = '" + tenantID + "'"); ddl += " AND SEQUENCE_NAME " + ((sequenceName == null) ? "IS NULL " : " = '" + sequenceName + "'"); ddl += " AND SEQUENCE_SCHEMA " + ((sequenceSchemaName == null) ? "IS NULL " : " = '" + sequenceSchemaName + "'" ); ResultSet rs = phxConn.createStatement().executeQuery(ddl); if(exists) { assertTrue(rs.next()); assertEquals(value, rs.getLong(4)); } else { assertFalse(rs.next()); } phxConn.close(); }
@Test public void testMaxMutationSize() throws Exception { Properties connectionProperties = new Properties(); connectionProperties.setProperty(QueryServices.MAX_MUTATION_SIZE_ATTRIB, "3"); connectionProperties.setProperty(QueryServices.MAX_MUTATION_SIZE_BYTES_ATTRIB, "50000"); connectionProperties.setProperty(QueryServices.ENABLE_SERVER_SIDE_MUTATIONS, allowServerSideMutations); PhoenixConnection connection = (PhoenixConnection) DriverManager.getConnection(getUrl(), connectionProperties); connection.setAutoCommit(true); String fullTableName = generateUniqueName(); try (Statement stmt = connection.createStatement()) { stmt.execute( "CREATE TABLE " + fullTableName + " (pk INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER)"); stmt.execute( "CREATE SEQUENCE " + fullTableName + "_seq cache 1000"); stmt.execute("UPSERT INTO " + fullTableName + " VALUES (NEXT VALUE FOR " + fullTableName + "_seq, rand(), rand())"); } try (Statement stmt = connection.createStatement()) { for (int i=0; i<16; i++) { stmt.execute("UPSERT INTO " + fullTableName + " SELECT NEXT VALUE FOR " + fullTableName + "_seq, rand(), rand() FROM " + fullTableName); } } connection.close(); }
@Test public void testCSVCommonsUpsert_NonExistentTable() throws Exception { PhoenixConnection conn = null; try { conn = DriverManager.getConnection(getUrl()).unwrap( PhoenixConnection.class); CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "NONEXISTENTTABLE", null, true, ',', '"', null, "!"); csvUtil.upsert( new StringReader("ID,VALARRAY\n" + "1,2!3!4\n")); fail("Trying to load a non-existent table should fail"); } catch (IllegalArgumentException e) { assertEquals("Table NONEXISTENTTABLE not found", e.getMessage()); } finally { if (conn != null) { conn.close(); } } } }
parser.close(); if (conn != null) conn.close();
@Test public void testCSVUpsertWithInvalidNumericalData_StrictMode() throws Exception { CSVParser parser = null; PhoenixConnection conn = null; try { String stockTableName = generateUniqueName(); // Create table String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY_ID BIGINT);"; conn = DriverManager.getConnection(getUrl()) .unwrap(PhoenixConnection.class); PhoenixRuntime.executeStatements(conn, new StringReader(statements), null); // Upsert CSV file in strict mode CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.asList("SYMBOL", "COMPANY_ID"), true); try { csvUtil.upsert(new StringReader(STOCK_CSV_VALUES)); fail("Running an upsert with data that can't be upserted in strict mode " + "should throw an exception"); } catch (IllegalDataException e) { // Expected } } finally { if (parser != null) parser.close(); if (conn != null) conn.close(); } }
conn.close();
X_DECIMAL = new ColumnRef(new TableRef(table), table.getColumnForColumnName("X_DECIMAL").getPosition()).newColumnExpression(); } finally { conn.close();