Skip to content

Commit f4f86fb

Browse files
committed
Use long-lived connection for StatsCollector
1 parent ea0f7b1 commit f4f86fb

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/main/java/eu/openanalytics/containerproxy/stat/impl/JDBCCollector.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,52 +34,53 @@
3434

3535
/**
3636
*
37-
* # MonetDB, Postgresql, MySQL/MariaDB
38-
* usage-stats-url: jdbc:monetdb://localhost:50000/usage_stats
39-
* usage-stats-url: jdbc:postgresql://localhost/postgres
40-
* usage-stats-url: jdbc:mysql://localhost/shinyproxy
37+
* # MonetDB, Postgresql, MySQL/MariaDB usage-stats-url:
38+
* jdbc:monetdb://localhost:50000/usage_stats usage-stats-url:
39+
* jdbc:postgresql://localhost/postgres usage-stats-url:
40+
* jdbc:mysql://localhost/shinyproxy
4141
*
4242
* Assumed table layout:
4343
*
44-
* create table event(
45-
* event_time timestamp,
46-
* username varchar(128),
47-
* type varchar(128),
48-
* data text
49-
* );
44+
* create table event( event_time timestamp, username varchar(128), type
45+
* varchar(128), data text );
5046
*
5147
*
52-
* # MS SQL Server
53-
* usage-stats-url: jdbc:sqlserver://localhost;databaseName=shinyproxy
48+
* # MS SQL Server usage-stats-url:
49+
* jdbc:sqlserver://localhost;databaseName=shinyproxy
5450
*
5551
* Assumed table layout:
5652
*
57-
* create table event(
58-
* event_time datetime,
59-
* username varchar(128),
60-
* type varchar(128),
61-
* data text
62-
* );
53+
* create table event( event_time datetime, username varchar(128), type
54+
* varchar(128), data text );
6355
*
6456
*/
6557
public class JDBCCollector implements IStatCollector {
6658

59+
private Connection conn;
60+
6761
@Override
6862
public void accept(Event event, Environment env) throws IOException {
69-
String username = env.getProperty("proxy.usage-stats-username", "monetdb");
70-
String password = env.getProperty("proxy.usage-stats-password", "monetdb");
71-
String baseURL = env.getProperty("proxy.usage-stats-url");
72-
try (Connection conn = DriverManager.getConnection(baseURL, username, password)) {
73-
String sql = "INSERT INTO event(event_time, username, type, data) VALUES (?,?,?,?)";
74-
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
75-
stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
76-
stmt.setString(2, event.user);
77-
stmt.setString(3, event.type);
78-
stmt.setString(4, event.data);
79-
stmt.executeUpdate();
63+
synchronized (this) {
64+
String baseURL = env.getProperty("proxy.usage-stats-url");
65+
String username = env.getProperty("proxy.usage-stats-username", "monetdb");
66+
String password = env.getProperty("proxy.usage-stats-password", "monetdb");
67+
try {
68+
if (conn == null || conn.isClosed()) {
69+
conn = DriverManager.getConnection(baseURL, username, password);
70+
}
71+
} catch (SQLException e) {
72+
throw new IOException("Failed to connect to " + baseURL, e);
8073
}
74+
}
75+
String sql = "INSERT INTO event(event_time, username, type, data) VALUES (?,?,?,?)";
76+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
77+
stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
78+
stmt.setString(2, event.user);
79+
stmt.setString(3, event.type);
80+
stmt.setString(4, event.data);
81+
stmt.executeUpdate();
8182
} catch (SQLException e) {
82-
throw new IOException("Failed to connect to " + baseURL, e);
83+
throw new IOException("Exception while loggin stats", e);
8384
}
8485
}
8586
}

0 commit comments

Comments
 (0)