Merge branch 'stable-2.12'

* stable-2.12:
  Evict idle connections after a configurable amount of time

Change-Id: If2d547a85d0162493da4bc2c22615001b103764e
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
index c39f71f..29e7299 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
@@ -39,6 +39,7 @@
   static final String CONFIG_PASSWORD = "storePassword";
   static final String CONFIG_WAIT_TIME = "retryTimeout";
   static final String CONFIG_CONN_TIME = "connectTimeout";
+  static final String CONFIG_EVICT_IDLE_TIME = "evictIdleTime";
 
   static final boolean DEFAULT_COPY_LOCAL = false;
   static final int DEFAULT_MAX_AGE = 30;
@@ -48,6 +49,7 @@
   static final int DEFAULT_CONN_TIME = 1000;
   static final String DEFAULT_DRIVER = "org.h2.Driver";
   static final String DEFAULT_URL = "jdbc:h2:~/db/";
+  static final int DEFAULT_EVICT_IDLE_TIME = 1000 * 60;
 
   private boolean copyLocal;
   private int maxAge;
@@ -61,6 +63,7 @@
   private String urlOptions;
   private String storeUsername;
   private String storePassword;
+  private int evictIdleTime;
 
   @Inject
   EventsLogConfig(PluginConfigFactory cfgFactory,
@@ -81,6 +84,7 @@
     urlOptions = cfg.getString(CONFIG_URL_OPTIONS, "");
     storeUsername = cfg.getString(CONFIG_USERNAME);
     storePassword = cfg.getString(CONFIG_PASSWORD);
+    evictIdleTime = cfg.getInt(CONFIG_EVICT_IDLE_TIME, DEFAULT_EVICT_IDLE_TIME);
   }
 
   public int getMaxAge() {
@@ -135,4 +139,8 @@
   public boolean getCopyLocal() {
     return copyLocal;
   }
+
+  public int getEvictIdleTime() {
+    return evictIdleTime;
+  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
index ffea0df..e4ccd9e 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
@@ -81,6 +81,17 @@
   }
 
   /**
+   * Set the time before an idle connection is evicted as well as the
+   * time between eviction runs.
+   *
+   * @param evictIdleTime the time in milliseconds before eviction
+   */
+  void setEvictIdleTime(int evictIdleTime) {
+    ds.setMinEvictableIdleTimeMillis(evictIdleTime);
+    ds.setTimeBetweenEvictionRunsMillis(evictIdleTime / 2);
+  }
+
+  /**
    * Create the database if it has not yet been created.
    *
    * @throws SQLException If there was a problem with the database
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLModule.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLModule.java
index 903ba57..2896942 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLModule.java
@@ -42,11 +42,11 @@
   @Singleton
   @EventsDb
   SQLClient provideSqlClient(EventsLogConfig cfg) {
-    SQLClient sqlClient =
-        new SQLClient(cfg.getStoreDriver(), cfg.getStoreUrl(),
-            cfg.getUrlOptions());
+    SQLClient sqlClient = new SQLClient(cfg.getStoreDriver(), cfg.getStoreUrl(),
+        cfg.getUrlOptions());
     sqlClient.setUsername(cfg.getStoreUsername());
     sqlClient.setPassword(cfg.getStorePassword());
+    sqlClient.setEvictIdleTime(cfg.getEvictIdleTime());
     return sqlClient;
   }
 
@@ -54,7 +54,9 @@
   @Singleton
   @LocalEventsDb
   SQLClient provideLocalSqlClient(EventsLogConfig cfg) {
-    return new SQLClient(cfg.getLocalStoreDriver(),
+    SQLClient sqlClient = new SQLClient(cfg.getLocalStoreDriver(),
         H2_DB_PREFIX + cfg.getLocalStorePath() + "/", cfg.getUrlOptions());
+    sqlClient.setEvictIdleTime(cfg.getEvictIdleTime());
+    return sqlClient;
   }
 }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index cc7f50e..69b7a46 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -63,4 +63,9 @@
      restored, set to true. The file will be copied to the same location as the
      backup database with a timestamp appended. Note that the copied file will
      not be deleted and must be removed manually. When not specified, the default
-     value is set to false.
\ No newline at end of file
+     value is set to false.
+
+plugin.@PLUGIN@.evictIdleTime
+:    Interval of time in milliseconds after which an idle database connection is
+     evicted from the connection pool. When not specified, the default value is
+     set to 60000ms.
\ No newline at end of file
diff --git a/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java b/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
index 6844e1e..461f197 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
@@ -16,6 +16,7 @@
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_CONN_TIME;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_COPY_LOCAL;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_DRIVER;
+import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_EVICT_IDLE_TIME;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_LOCAL_PATH;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_MAX_AGE;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.CONFIG_MAX_TRIES;
@@ -28,6 +29,7 @@
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_CONN_TIME;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_COPY_LOCAL;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_DRIVER;
+import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_EVICT_IDLE_TIME;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_MAX_AGE;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_MAX_TRIES;
 import static com.ericsson.gerrit.plugins.eventslog.EventsLogConfig.DEFAULT_RETURN_LIMIT;
@@ -59,6 +61,8 @@
   private String defaultLocalStorePath;
   private String localStorePath;
 
+  private static final int CUSTOM_EVICT_IDLE_TIME = 10000;
+
   @Rule
   public TemporaryFolder gerrit_site = new TemporaryFolder();
 
@@ -87,6 +91,7 @@
     expect(configMock.getString(CONFIG_URL_OPTIONS, "")).andReturn("");
     expect(configMock.getString(CONFIG_USERNAME)).andReturn(null);
     expect(configMock.getString(CONFIG_PASSWORD)).andReturn(null);
+    expect(configMock.getInt(CONFIG_EVICT_IDLE_TIME, DEFAULT_EVICT_IDLE_TIME)).andReturn(DEFAULT_EVICT_IDLE_TIME);
 
     easyMock.replayAll();
   }
@@ -105,6 +110,7 @@
     expect(configMock.getString(CONFIG_URL_OPTIONS, "")).andReturn("DB_CLOSE_DELAY=10");
     expect(configMock.getString(CONFIG_USERNAME)).andReturn("testUsername");
     expect(configMock.getString(CONFIG_PASSWORD)).andReturn("testPassword");
+    expect(configMock.getInt(CONFIG_EVICT_IDLE_TIME, DEFAULT_EVICT_IDLE_TIME)).andReturn(CUSTOM_EVICT_IDLE_TIME);
 
     easyMock.replayAll();
   }
@@ -126,6 +132,7 @@
     assertThat(config.getUrlOptions()).isEmpty();
     assertThat(config.getStoreUsername()).isNull();
     assertThat(config.getStorePassword()).isNull();
+    assertThat(config.getEvictIdleTime()).isEqualTo(DEFAULT_EVICT_IDLE_TIME);
   }
 
   @Test
@@ -145,5 +152,6 @@
     assertThat(config.getUrlOptions()).isEqualTo("DB_CLOSE_DELAY=10");
     assertThat(config.getStoreUsername()).isEqualTo("testUsername");
     assertThat(config.getStorePassword()).isEqualTo("testPassword");
+    assertThat(config.getEvictIdleTime()).isEqualTo(CUSTOM_EVICT_IDLE_TIME);
   }
 }