好的,这是我的解决方案。在 Javers 工作完成后收集 Javers 获得的所有连接并关闭它们(close() 不会关闭而是将它们释放到连接池中!):
收集连接的类:
public class ConnectionCleanup {
private static final Logger LOG = LoggerFactory.getLogger(ConnectionCleanup.class);
private static final ThreadLocal<List<Connection>> connections = new ThreadLocal<>();
public static void add(final Connection connection) {
List<Connection> listConnections = connections.get();
if (listConnections == null) {
listConnections = new ArrayList<>();
connections.set(listConnections);
}
listConnections.add(connection);
}
public static void cleanup() {
final List<Connection> listConnections = connections.get();
if (listConnections == null) {
return;
}
for (final Connection con : listConnections) {
try {
if (!con.isClosed()) {
con.close();
}
} catch (final Exception ex) {
LOG.error("Caught an", ex);
}
}
connections.set(null);
}
}
在 Javers 工作完成后包装 SqlRepository 以调用 cleanup:
public class CleanupJaversSqlRepository implements JaversRepository {
private final JaversSqlRepository repository;
public CleanupJaversSqlRepository(final JaversSqlRepository repository) {
this.repository = repository;
}
@Override
public Optional<CdoSnapshot> getLatest(final GlobalId globalId) {
final Optional<CdoSnapshot> latest = repository.getLatest(globalId);
ConnectionCleanup.cleanup();
return latest;
}
@Override
public void persist(final Commit commit) {
repository.persist(commit);
ConnectionCleanup.cleanup();
}
@Override
public CommitId getHeadId() {
final CommitId commitId = repository.getHeadId();
ConnectionCleanup.cleanup();
return commitId;
}
@Override
public void setJsonConverter(final JsonConverter jsonConverter) {
repository.setJsonConverter(jsonConverter);
ConnectionCleanup.cleanup();
}
@Override
public void ensureSchema() {
repository.ensureSchema();
ConnectionCleanup.cleanup();
}
@Override
public List<CdoSnapshot> getStateHistory(
final GlobalId globalId,
final QueryParams queryParams) {
final List<CdoSnapshot> stateHistory = repository.getStateHistory(globalId, queryParams);
ConnectionCleanup.cleanup();
return stateHistory;
}
@Override
public List<CdoSnapshot> getPropertyStateHistory(
final GlobalId globalId,
final String propertyName,
final QueryParams queryParams) {
final List<CdoSnapshot> stateHistory = repository.getPropertyStateHistory(
globalId, propertyName, queryParams);
ConnectionCleanup.cleanup();
return stateHistory;
}
@Override
public List<CdoSnapshot> getStateHistory(
final ManagedType givenClass,
final QueryParams queryParams) {
final List<CdoSnapshot> stateHistory = repository.getStateHistory(givenClass, queryParams);
ConnectionCleanup.cleanup();
return stateHistory;
}
@Override
public List<CdoSnapshot> getPropertyStateHistory(
final ManagedType givenClass,
final String propertyName,
final QueryParams queryParams) {
final List<CdoSnapshot> stateHistory = repository.getPropertyStateHistory(
givenClass, propertyName, queryParams);
ConnectionCleanup.cleanup();
return stateHistory;
}
@Override
public List<CdoSnapshot> getValueObjectStateHistory(
final EntityType ownerEntity,
final String path,
final QueryParams queryParams) {
final List<CdoSnapshot> stateHistory = repository.getValueObjectStateHistory(
ownerEntity, path, queryParams);
ConnectionCleanup.cleanup();
return stateHistory;
}
}
最后,把它们放在一起:
JaversBuilder.javers().
registerJaversRepository(new CleanupJaversSqlRepository(SqlRepositoryBuilder.sqlRepository().
withConnectionProvider(() -> {
final Connection connection = ((SessionFactoryImpl) sessionFactory).
getServiceRegistry().getService(
org.hibernate.engine.jdbc.connections.spi.ConnectionProvider.class).
getConnection();
ConnectionCleanup.add(connection);
return connection;
}).
withDialect(DialectName.MSSQL).
build())).
build();