-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug: mysql statement leak #1627
Conversation
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you for your contribution.
hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlEntryIterator.java
Outdated
Show resolved
Hide resolved
@@ -65,8 +65,8 @@ protected final boolean fetch() { | |||
} | |||
|
|||
try { | |||
while (!this.results.isClosed() && this.results.next()) { | |||
MysqlBackendEntry entry = this.row2Entry(this.results); | |||
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer to add hasNext method to ResultSetWrapper to wrap isClosed() and next()
while (!this.results.isClosed() && this.results.next()) { | ||
MysqlBackendEntry entry = this.row2Entry(this.results); | ||
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) { | ||
MysqlBackendEntry entry = this.row2Entry(this.resultSetWrapper.getResultSet()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to next()
@@ -672,8 +672,8 @@ protected void wrapOffset(StringBuilder select, Query query) { | |||
} | |||
|
|||
protected Iterator<BackendEntry> results2Entries(Query query, | |||
ResultSet results) { | |||
return new MysqlEntryIterator(results, query, this::mergeEntries); | |||
ResultSetWrapper resultSetWrapper) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep results name
try { | ||
ResultSet resultSet = session.select(select); | ||
resultSetWrapper = session.select(select); | ||
ResultSet resultSet = resultSetWrapper.getResultSet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to ResultSet rs
?
@@ -196,21 +197,22 @@ private PaloFile getOrCreate(String table) { | |||
@SuppressWarnings("unused") | |||
private PaloLoadInfo getLoadInfoByLabel(String label) { | |||
String sql = String.format("SHOW LOAD WHERE LABEL = '%s'", label); | |||
ResultSet result; | |||
ResultSetWrapper resultSetWrapper = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
if (result.next()) { | ||
return new PaloLoadInfo(result); | ||
resultSetWrapper = this.select(sql); | ||
ResultSet resultSet = resultSetWrapper.getResultSet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -364,8 +364,8 @@ public Number queryNumber(Session session, Query query) { | |||
List<StringBuilder> selections = this.query2Select(this.table(), query); | |||
try { | |||
for (StringBuilder selection : selections) { | |||
ResultSet results = session.select(selection.toString()); | |||
rs.extend(parser.apply(query, results)); | |||
ResultSetWrapper resultSetWrapper = session.select(selection.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep results name
@@ -486,7 +493,17 @@ public boolean execute(String sql) throws SQLException { | |||
if (!this.conn.getAutoCommit()) { | |||
this.end(); | |||
} | |||
return this.conn.createStatement().execute(sql); | |||
Statement statement = null; | |||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use try (Statement statement = this.conn.createStatement())
} finally { | ||
if (resultSetWrapper != null) { | ||
resultSetWrapper.close(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems lack of ResultSetWrapper file
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Codecov Report
@@ Coverage Diff @@
## master #1627 +/- ##
============================================
+ Coverage 62.81% 63.18% +0.36%
- Complexity 6691 6715 +24
============================================
Files 418 419 +1
Lines 34504 34527 +23
Branches 4778 4779 +1
============================================
+ Hits 21674 21816 +142
+ Misses 10581 10456 -125
- Partials 2249 2255 +6
Continue to review full report at Codecov.
|
@@ -65,8 +65,8 @@ protected final boolean fetch() { | |||
} | |||
|
|||
try { | |||
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) { | |||
MysqlBackendEntry entry = this.row2Entry(this.resultSetWrapper.getResultSet()); | |||
while (!this.results.isClosed() && this.results.next()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move !this.results.isClosed()
into next() method
return resultSet.next(); | ||
} | ||
|
||
public boolean isClosed() throws SQLException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge into next()
} | ||
} | ||
|
||
public ResultSet getResultSet() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep resultSet() style
return resultSet; | ||
} | ||
|
||
public void setResultSet(ResultSet resultSet) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove it
public Statement getStatement() { | ||
return statement; | ||
} | ||
|
||
public void setStatement(Statement statement) { | ||
this.statement = statement; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove them
private ResultSet resultSet; | ||
private Statement statement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set final
Statement statement = this.conn.createStatement(); | ||
ResultSet rs = statement.executeQuery(sql); | ||
return new ResultSetWrapper(rs, statement); | ||
} catch (SQLException sqlException) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQLException e
assert this.conn.getAutoCommit(); | ||
return this.conn.createStatement().executeQuery(sql); | ||
try { | ||
Statement statement = this.conn.createStatement(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move out of try-catch
ResultSet rs = statement.executeQuery(sql); | ||
return new ResultSetWrapper(rs, statement); | ||
} catch (SQLException sqlException) { | ||
throw sqlException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close statement if exception occurred when executeQuery()
try { | ||
ResultSet resultSet = session.select(select); | ||
results = session.select(select); | ||
ResultSet resultSet = results.getResultSet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResultSet rs
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
public ResultSet resultSet() { | ||
return resultSet; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete empty line
} | ||
} catch (SQLException e) { | ||
throw new BackendException( | ||
"Failed to close resultSet and statement", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to close resultSet or Statement
also prefer align " with BackendException in prior line
@@ -66,7 +66,7 @@ protected final boolean fetch() { | |||
|
|||
try { | |||
while (!this.results.isClosed() && this.results.next()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can remove !this.results.isClosed() &&
since done in results.next()
} | ||
} catch (SQLException e) { | ||
throw new BackendException( | ||
"Failed to close resultSet and statement", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align with "new" and keep "ResultSet and Statement"
if (this.statement != null) { | ||
this.statement.close(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to finally-block
if (result.next()) { | ||
return new PaloLoadInfo(result); | ||
try (ResultSetWrapper results = this.select(sql)){ | ||
ResultSet resultSet = results.resultSet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also keep ResultSet rs
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak fix apache#1626
Fix bug: mysql statement leak
fix #1626