From ea39cf9cb2cb212c886ee5a6d6ac353cd7606da9 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Tue, 24 Jul 2018 19:06:55 +0400 Subject: [PATCH 1/2] JAMES-2498 JDBCMailRepository should not materialize entities for counting them. Executing SQL query for size. --- .../jdbc/JDBCMailRepository.java | 21 +++++++++++++++++++ .../src/test/resources/sqlResources.xml | 3 +++ 2 files changed, 24 insertions(+) diff --git a/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java b/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java index 31a728eab0e..1b3d8994bae 100644 --- a/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java +++ b/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java @@ -721,6 +721,27 @@ protected void internalRemove(MailKey key) throws MessagingException { } } + @Override + public long size() throws MessagingException { + Connection conn = null; + PreparedStatement count = null; + ResultSet resultSet = null; + + try { + conn = datasource.getConnection(); + count = conn.prepareStatement(sqlQueries.getSqlString("countMessagesSQL", true)); + resultSet = count.executeQuery(); + + return resultSet.next() ? resultSet.getLong(1) : 0; + } catch (Exception e) { + throw new MessagingException("Exception while removing mail: " + e.getMessage(), e); + } finally { + theJDBCUtil.closeJDBCResultSet(resultSet); + theJDBCUtil.closeJDBCStatement(count); + theJDBCUtil.closeJDBCConnection(conn); + } + } + @Override public Iterator list() throws MessagingException { // System.err.println("listing messages"); diff --git a/server/data/data-jdbc/src/test/resources/sqlResources.xml b/server/data/data-jdbc/src/test/resources/sqlResources.xml index 4fe890c8e21..b91f974bb7d 100644 --- a/server/data/data-jdbc/src/test/resources/sqlResources.xml +++ b/server/data/data-jdbc/src/test/resources/sqlResources.xml @@ -287,6 +287,9 @@ SELECT message_name, message_state, last_updated FROM ${table} WHERE repository_name = ? ORDER BY last_updated ASC + + SELECT COUNT(*) FROM ${table} + CREATE TABLE ${table} ( From 5b0209d8d8a356599b219391d6e70c96788bfe10 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Wed, 25 Jul 2018 12:24:48 +0400 Subject: [PATCH 2/2] fixup! JAMES-2498 JDBCMailRepository should not materialize entities for counting them. --- .../mailrepository/jdbc/JDBCMailRepository.java | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java b/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java index 1b3d8994bae..9875676b8ea 100644 --- a/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java +++ b/server/data/data-jdbc/src/main/java/org/apache/james/mailrepository/jdbc/JDBCMailRepository.java @@ -723,22 +723,13 @@ protected void internalRemove(MailKey key) throws MessagingException { @Override public long size() throws MessagingException { - Connection conn = null; - PreparedStatement count = null; - ResultSet resultSet = null; - - try { - conn = datasource.getConnection(); - count = conn.prepareStatement(sqlQueries.getSqlString("countMessagesSQL", true)); - resultSet = count.executeQuery(); + try (Connection conn = datasource.getConnection(); + PreparedStatement count = conn.prepareStatement(sqlQueries.getSqlString("countMessagesSQL", true)); + ResultSet resultSet = count.executeQuery()) { return resultSet.next() ? resultSet.getLong(1) : 0; } catch (Exception e) { - throw new MessagingException("Exception while removing mail: " + e.getMessage(), e); - } finally { - theJDBCUtil.closeJDBCResultSet(resultSet); - theJDBCUtil.closeJDBCStatement(count); - theJDBCUtil.closeJDBCConnection(conn); + throw new MessagingException("Exception while fetching size: " + e.getMessage(), e); } }