You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2020/10/30 02:33:05 UTC

[GitHub] [phoenix] yanxinyi commented on a change in pull request #943: PHOENIX-6087 : Resolve Phoenix Connection leak in UpgradeUtil.addViewIndexToParentLinks() and Admin leak in UpgradeUtil.syncTableAndIndexProperties()

yanxinyi commented on a change in pull request #943:
URL: https://github.com/apache/phoenix/pull/943#discussion_r514726107



##########
File path: phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java
##########
@@ -1211,36 +1213,56 @@ public static void addViewIndexToParentLinks(PhoenixConnection oldMetaConnection
 			String createViewIndexLink = "UPSERT INTO SYSTEM.CATALOG (TENANT_ID, TABLE_SCHEM, TABLE_NAME, COLUMN_FAMILY, LINK_TYPE) VALUES (?,?,?,?,?) ";
             ResultSet rs = queryConn.createStatement().executeQuery(indexQuery);
             String prevTenantId = null;
-            PhoenixConnection metaConn = queryConn;
+            metaConn = queryConn;
             Properties props = new Properties(queryConn.getClientInfo());
-			props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(HConstants.LATEST_TIMESTAMP));
+            props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB,
+                Long.toString(HConstants.LATEST_TIMESTAMP));
             while (rs.next()) {
-            	String tenantId = rs.getString("TENANT_ID");
-				if (!java.util.Objects.equals(prevTenantId, tenantId)) {
-					prevTenantId = tenantId;
-					props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
-            		metaConn = new PhoenixConnection(oldMetaConnection, props); 
-            	}
-            	String schemaName = rs.getString("TABLE_SCHEM");
-            	String parentTableName = rs.getString("TABLE_NAME");
-            	String fullParentTableName = SchemaUtil.getTableName(schemaName, parentTableName);
-            	String indexName = rs.getString("COLUMN_FAMILY");
-            	PTable table = PhoenixRuntime.getTable(metaConn, fullParentTableName);
-            	if (table==null) {
-            		throw new TableNotFoundException(fullParentTableName);
-            	}
-            	if (table.getType().equals(PTableType.VIEW)) {
-            		PreparedStatement prepareStatement = upsertConn.prepareStatement(createViewIndexLink);
-            		prepareStatement.setString(1, tenantId);
-            		prepareStatement.setString(2, schemaName);
-            		prepareStatement.setString(3, indexName);
-            		prepareStatement.setString(4, parentTableName);
-            		prepareStatement.setByte(5, LinkType.VIEW_INDEX_PARENT_TABLE.getSerializedValue());
-            		prepareStatement.execute();
-            		upsertConn.commit();
-            	}
+                String tenantId = rs.getString("TENANT_ID");
+                if (!java.util.Objects.equals(prevTenantId, tenantId)) {
+                    prevTenantId = tenantId;
+                    props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
+                    // guard again queryConn because we don't want to close
+                    // queryConn if metaConn was assigned queryConn at
+                    // this point
+                    if (!isMetaConnUsingQueryConn) {
+                        metaConn.close();
+                    }
+                    metaConn = new PhoenixConnection(oldMetaConnection, props);
+                    // now that we have reassigned metaConn, make
+                    // isMetaConnUsingQueryConn false so that we make
+                    // metaConn eligible for closure
+                    isMetaConnUsingQueryConn = false;
+                }
+                String schemaName = rs.getString("TABLE_SCHEM");

Review comment:
       please using `PhoenixDatabaseMetaData.TABLE_SCHEM` constant




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org