Created
February 25, 2016 05:51
-
-
Save dotmanila/2328465c930e3c555b4f to your computer and use it in GitHub Desktop.
Sample Java Source Code utilizing connection pools fom DataSource.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
import java.beans.PropertyVetoException; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.time.Instant; | |
public class C3P0DataSourceExample { | |
public static void main(String[] args) throws PropertyVetoException, SQLException, IOException { | |
Connection connection = null; | |
Statement statement = null; | |
ResultSet resultSet = null; | |
while (true) { | |
try { | |
connection = DataSource.getInstance().getConnection(); | |
statement = connection.createStatement(); | |
resultSet = statement.executeQuery("select connection_id() as id, from_unixtime(unix_timestamp()) as t, unix_timestamp() as n"); | |
while (resultSet.next()) { | |
long unixTimestamp = Instant.now().getEpochSecond(); | |
long diff = unixTimestamp - resultSet.getLong("n"); | |
String isThereDiff = (diff >= 30) ? " Time went back > 30 seconds!" : " --"; | |
System.out.println("id: " + resultSet.getString("id") + " t: " + resultSet.getString("t") + isThereDiff); | |
} | |
Thread.sleep(500); | |
} catch(InterruptedException ex) { | |
Thread.currentThread().interrupt(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
if (resultSet != null) try { resultSet.close(); } catch (SQLException e) {e.printStackTrace();} | |
if (statement != null) try { statement.close(); } catch (SQLException e) {e.printStackTrace();} | |
if (connection != null) try { connection.close(); } catch (SQLException e) {e.printStackTrace();} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment