|
29 | 29 |
|
30 | 30 | import org.springframework.core.env.Environment;
|
31 | 31 |
|
| 32 | +import com.zaxxer.hikari.HikariDataSource; |
| 33 | + |
32 | 34 | import eu.openanalytics.containerproxy.service.EventService.Event;
|
33 | 35 | import eu.openanalytics.containerproxy.stat.IStatCollector;
|
34 | 36 |
|
|
56 | 58 | */
|
57 | 59 | public class JDBCCollector implements IStatCollector {
|
58 | 60 |
|
59 |
| - private Connection conn; |
| 61 | + private HikariDataSource ds; |
| 62 | + |
| 63 | + public JDBCCollector(Environment environment) { |
| 64 | + String baseURL = environment.getProperty("proxy.usage-stats-url"); |
| 65 | + String username = environment.getProperty("proxy.usage-stats-username", "monetdb"); |
| 66 | + String password = environment.getProperty("proxy.usage-stats-password", "monetdb"); |
| 67 | + ds = new HikariDataSource(); |
| 68 | + ds.setJdbcUrl(baseURL); |
| 69 | + ds.setUsername(username); |
| 70 | + ds.setPassword(password); |
| 71 | + |
| 72 | + Long connectionTimeout = environment.getProperty("proxy.usage-stats-hikari.connection-timeout", Long.class); |
| 73 | + if (connectionTimeout != null) { |
| 74 | + ds.setConnectionTimeout(connectionTimeout); |
| 75 | + } |
| 76 | + |
| 77 | + Long idleTimeout = environment.getProperty("proxy.usage-stats-hikari.idle-timeout", Long.class); |
| 78 | + if (idleTimeout != null) { |
| 79 | + ds.setIdleTimeout(idleTimeout); |
| 80 | + } |
| 81 | + |
| 82 | + Long maxLifetime = environment.getProperty("proxy.usage-stats-hikari.max-lifetime", Long.class); |
| 83 | + if (maxLifetime != null) { |
| 84 | + ds.setMaxLifetime(maxLifetime); |
| 85 | + } |
| 86 | + |
| 87 | + Integer minimumIdle = environment.getProperty("proxy.usage-stats-hikari.minimum-idle", Integer.class); |
| 88 | + if (minimumIdle != null) { |
| 89 | + ds.setMinimumIdle(minimumIdle); |
| 90 | + } |
| 91 | + |
| 92 | + Integer maximumPoolSize = environment.getProperty("proxy.usage-stats-hikari.maximum-pool-size", Integer.class); |
| 93 | + if (maximumPoolSize != null) { |
| 94 | + ds.setMaximumPoolSize(maximumPoolSize); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
60 | 99 |
|
61 | 100 | @Override
|
62 | 101 | public void accept(Event event, Environment env) throws IOException {
|
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); |
73 |
| - } |
74 |
| - } |
75 | 102 | 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(); |
| 103 | + try (Connection con = ds.getConnection()) { |
| 104 | + try (PreparedStatement stmt = con.prepareStatement(sql)) { |
| 105 | + stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis())); |
| 106 | + stmt.setString(2, event.user); |
| 107 | + stmt.setString(3, event.type); |
| 108 | + stmt.setString(4, event.data); |
| 109 | + stmt.executeUpdate(); |
| 110 | + } |
82 | 111 | } catch (SQLException e) {
|
83 | 112 | throw new IOException("Exception while loggin stats", e);
|
84 | 113 | }
|
|
0 commit comments