Created
December 3, 2015 11:44
-
-
Save ivanursul/cd62797a21d9a6d8ccfa to your computer and use it in GitHub Desktop.
Main.java
This file contains hidden or 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
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