Skip to content

Instantly share code, notes, and snippets.

@cowwoc
Created November 21, 2014 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowwoc/563b4e0ade2c455049f4 to your computer and use it in GitHub Desktop.
Save cowwoc/563b4e0ade2c455049f4 to your computer and use it in GitHub Desktop.
Hikari testcase
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.CountDownLatch;
/**
* @author Gili Tzabari
*/
public class Main {
public static void main(String[] args) {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:postgresql://localhost/test");
dataSource.setUsername("test");
dataSource.setPassword("test");
dataSource.setMaximumPoolSize((Runtime.getRuntime().availableProcessors() * 2) + 1);
dataSource.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
dataSource.setLeakDetectionThreshold(30_000);
CountDownLatch shutdownThreadReady = new CountDownLatch(1);
CountDownLatch readyForShutdown = new CountDownLatch(1);
Thread thread1 = new Thread(() -> {
try {
shutdownThreadReady.await();
Connection connection = dataSource.getConnection();
readyForShutdown.countDown();
// Uncomment to trigger SQLState(08003) and
// "Attempt to remove an object from the bag that was not borrowed or reserved"
//Thread.sleep(100);
System.err.println("*** pre-close");
connection.close();
System.err.println("*** post-close");
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
shutdownThreadReady.countDown();
try {
readyForShutdown.await();
// Uncomment to trigger "PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception."
//Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("*** pre-shutdown");
dataSource.shutdown();
System.err.println("*** post-shutdown");
});
thread1.start();
thread2.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment