Skip to content

Instantly share code, notes, and snippets.

@ivanursul
Created December 3, 2015 11:44
Show Gist options
  • Save ivanursul/cd62797a21d9a6d8ccfa to your computer and use it in GitHub Desktop.
Save ivanursul/cd62797a21d9a6d8ccfa to your computer and use it in GitHub Desktop.
Main.java
package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 400000; i++) {
try (Person person = new Person(getConnection())) {
try {
System.out.println(person.getConnectionUrl());
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
protected static Connection getConnection() {
Connection connection;
try {
Properties properties = new Properties();
properties.setProperty("user", "postgres");
properties.setProperty("password", "postgres");
properties.setProperty("defaultSchemaName", "test");
properties.setProperty("currentSchema", "reporting");
properties.setProperty("socketTimeout", "3600");
properties.setProperty("minSize", "2");
properties.setProperty("driverClass", "org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", properties);
} catch (SQLException e) {
throw new RuntimeException("Connection exception");
}
return connection;
}
}
class Person implements AutoCloseable {
private Connection connection;
public Person(Connection connection) {
this.connection = connection;
}
@Override
public void close() {
System.out.println("Closing");
/*
try {
if (!connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}*/
}
public String getConnectionUrl() throws SQLException {
return connection.getMetaData().getURL();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment