|
34 | 34 |
|
35 | 35 | /**
|
36 | 36 | *
|
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 |
41 | 41 | *
|
42 | 42 | * Assumed table layout:
|
43 | 43 | *
|
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 ); |
50 | 46 | *
|
51 | 47 | *
|
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 |
54 | 50 | *
|
55 | 51 | * Assumed table layout:
|
56 | 52 | *
|
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 ); |
63 | 55 | *
|
64 | 56 | */
|
65 | 57 | public class JDBCCollector implements IStatCollector {
|
66 | 58 |
|
| 59 | + private Connection conn; |
| 60 | + |
67 | 61 | @Override
|
68 | 62 | 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); |
80 | 73 | }
|
| 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(); |
81 | 82 | } catch (SQLException e) {
|
82 |
| - throw new IOException("Failed to connect to " + baseURL, e); |
| 83 | + throw new IOException("Exception while loggin stats", e); |
83 | 84 | }
|
84 | 85 | }
|
85 | 86 | }
|
0 commit comments