-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leak with one-shot connection factories (#1311)
Fixes #1302 After creating an unpooled connection, add it as a close hook to the creating context. Also, in this case, make sure the connection factory is closed after usage. Signed-off-by: Thomas Segismont <[email protected]>
- Loading branch information
1 parent
769a6d2
commit d542a61
Showing
9 changed files
with
116 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,18 @@ | |
|
||
package io.vertx.sqlclient.impl; | ||
|
||
import io.vertx.core.*; | ||
import io.vertx.core.impl.ContextInternal; | ||
import io.vertx.core.impl.future.PromiseInternal; | ||
import io.vertx.core.spi.metrics.ClientMetrics; | ||
import io.vertx.core.spi.tracing.VertxTracer; | ||
import io.vertx.sqlclient.PrepareOptions; | ||
import io.vertx.sqlclient.PreparedStatement; | ||
import io.vertx.sqlclient.SqlConnection; | ||
import io.vertx.sqlclient.Transaction; | ||
import io.vertx.sqlclient.impl.command.*; | ||
import io.vertx.core.*; | ||
import io.vertx.sqlclient.impl.command.CommandBase; | ||
import io.vertx.sqlclient.impl.command.PrepareStatementCommand; | ||
import io.vertx.sqlclient.impl.command.QueryCommandBase; | ||
import io.vertx.sqlclient.impl.pool.SqlConnectionPool; | ||
import io.vertx.sqlclient.impl.tracing.QueryReporter; | ||
import io.vertx.sqlclient.spi.ConnectionFactory; | ||
|
@@ -35,10 +38,11 @@ | |
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class SqlConnectionBase<C extends SqlConnectionBase<C>> extends SqlClientBase implements SqlConnectionInternal { | ||
public class SqlConnectionBase<C extends SqlConnectionBase<C>> extends SqlClientBase implements SqlConnectionInternal, Closeable { | ||
|
||
private volatile Handler<Throwable> exceptionHandler; | ||
private volatile Handler<Void> closeHandler; | ||
private volatile boolean closeFactoryAfterUsage; | ||
protected TransactionImpl tx; | ||
protected final ContextInternal context; | ||
protected final ConnectionFactory factory; | ||
|
@@ -192,7 +196,16 @@ public Future<Void> close() { | |
return promise.future(); | ||
} | ||
|
||
private void close(Promise<Void> promise) { | ||
@Override | ||
public void close(Promise<Void> completion) { | ||
doClose(completion); | ||
if (closeFactoryAfterUsage) { | ||
completion.future().onComplete(v -> factory.close(Promise.promise())); | ||
context.removeCloseHook(this); | ||
} | ||
} | ||
|
||
private void doClose(Promise<Void> promise) { | ||
context.execute(promise, p -> { | ||
if (tx != null) { | ||
tx.rollback(ar -> conn.close(this, p)); | ||
|
@@ -202,4 +215,14 @@ private void close(Promise<Void> promise) { | |
} | ||
}); | ||
} | ||
|
||
protected static Future<SqlConnection> prepareForClose(ContextInternal ctx, Future<SqlConnection> future) { | ||
return future.andThen(ar -> { | ||
if (ar.succeeded()) { | ||
SqlConnectionBase<?> base = (SqlConnectionBase<?>) ar.result(); | ||
base.closeFactoryAfterUsage = true; | ||
ctx.addCloseHook(base); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters