到tackle connection leaks is to do it during testing的最佳方式。
您可以使用自动化实用程序,以便每个测试都会验证是否存在连接泄漏。
@BeforeClass
public static void initConnectionLeakUtility() {
if (enableConnectionLeakDetection) {
connectionLeakUtil = new ConnectionLeakUtil();
}
}
@AfterClass
public static void assertNoLeaks() {
if (enableConnectionLeakDetection) {
connectionLeakUtil.assertNoLeaks();
}
}
的ConnectionLeakUtil
看起来是这样的:
public class ConnectionLeakUtil {
private JdbcProperties jdbcProperties = JdbcProperties.INSTANCE;
private List idleConnectionCounters =
Arrays.asList(
H2IdleConnectionCounter.INSTANCE,
OracleIdleConnectionCounter.INSTANCE,
PostgreSQLIdleConnectionCounter.INSTANCE,
MySQLIdleConnectionCounter.INSTANCE
);
private IdleConnectionCounter connectionCounter;
private int connectionLeakCount;
public ConnectionLeakUtil() {
for (IdleConnectionCounter connectionCounter :
idleConnectionCounters) {
if (connectionCounter.appliesTo(
Dialect.getDialect().getClass())) {
this.connectionCounter = connectionCounter;
break;
}
}
if (connectionCounter != null) {
connectionLeakCount = countConnectionLeaks();
}
}
public void assertNoLeaks() {
if (connectionCounter != null) {
int currentConnectionLeakCount = countConnectionLeaks();
int diff = currentConnectionLeakCount - connectionLeakCount;
if (diff > 0) {
throw new ConnectionLeakException(
String.format(
"%d connection(s) have been leaked! Previous leak count: %d, Current leak count: %d",
diff,
connectionLeakCount,
currentConnectionLeakCount
)
);
}
}
}
private int countConnectionLeaks() {
try (Connection connection = newConnection()) {
return connectionCounter.count(connection);
}
catch (SQLException e) {
throw new IllegalStateException(e);
}
}
private Connection newConnection() {
try {
return DriverManager.getConnection(
jdbcProperties.getUrl(),
jdbcProperties.getUser(),
jdbcProperties.getPassword()
);
}
catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
的IdleConnectionCounter
实现可以在这个blog post发现,和MySQL的版本是这样的:
public class MySQLIdleConnectionCounter implements IdleConnectionCounter {
public static final IdleConnectionCounter INSTANCE =
new MySQLIdleConnectionCounter();
@Override
public boolean appliesTo(Class<? extends Dialect> dialect) {
return MySQL5Dialect.class.isAssignableFrom(dialect);
}
@Override
public int count(Connection connection) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(
"SHOW PROCESSLIST")) {
int count = 0;
while (resultSet.next()) {
String state = resultSet.getString("command");
if ("sleep".equalsIgnoreCase(state)) {
count++;
}
}
return count;
}
}
catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
现在,当您运行测试,当连接泄漏时您将失败:
:hibernate-core:test
org.hibernate.jpa.test.EntityManagerFactoryClosedTest > classMethod FAILED
org.hibernate.testing.jdbc.leak.ConnectionLeakException
你的应用程序以什么语言编码? – 2011-04-21 09:03:03
Asp.net。为了反映这一点,我编辑了原始问题。 – thomasb 2011-04-21 10:19:27