[java-opensaml] 08/08: OSJ-342 Investigate Strategies to end of life our use of Hibernate in V5

Rod Widdowson rdw at steadingsoftware.com
Sun May 15 14:59:22 UTC 2022


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch dev/OSJ-342
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=19a1e9988344d5acfce7211784192e5ffc18a416

commit 19a1e9988344d5acfce7211784192e5ffc18a416
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun May 15 15:58:13 2022 +0100

    OSJ-342 Investigate Strategies to end of life our use of Hibernate in V5
    
    https://shibboleth.atlassian.net/browse/OSJ-342
    
    Add constraints checking of parameters.
    Add logging (at trace) of the SQL.
---
 .../opensaml/storage/impl/JDBCStorageService.java  | 110 ++++++++++++---------
 1 file changed, 66 insertions(+), 44 deletions(-)

diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JDBCStorageService.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JDBCStorageService.java
index ba15ade5c..aff37ccc3 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JDBCStorageService.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JDBCStorageService.java
@@ -366,7 +366,7 @@ public final class JDBCStorageService extends AbstractStorageService implements
 
     /** {@inheritDoc} */
     protected void doInitialize() throws ComponentInitializationException {
-        Constraint.isNotNull(dataSource, "data source must be specified and nonnul");
+        Constraint.isNotNull(dataSource, "data source must be specified and non-null");
         super.doInitialize();
     }
 
@@ -378,19 +378,19 @@ public final class JDBCStorageService extends AbstractStorageService implements
      */
     @Nonnull @NonnullElements protected List<String> readContexts() throws IOException {
         final List<String> result = new ArrayList<>();
-        log.trace("Getting Context");
         try (final Connection connection = getConnection(true)) {
+            log.trace("ReadContexts:: ", readContextsSQL);
             final PreparedStatement query = connection.prepareStatement(readContextsSQL);
             final ResultSet results = query.executeQuery();
             while (results.next()) {
                 final String context = results.getString(1);
-                log.trace("Context = {}", context);
+                log.trace("Context = '{}'", context);
                 result.add(context);
             }
             return result;
             
         } catch (final SQLException e) {
-            log.error("ReadAll()", e);
+            log.error("ReadContexts failed", e);
             throw new IOException(e);
         }
     }
@@ -403,8 +403,8 @@ public final class JDBCStorageService extends AbstractStorageService implements
      */
     @Nonnull @NonnullElements protected List<?> readAll() throws IOException {
         final List<JDBCStorageRecord<?>> result = new ArrayList<>();
-        log.trace("Getting all Records");
         try (final Connection connection = getConnection(true)) {
+            log.trace("ReadAll:: '{}' ", readAllSQL);
             final PreparedStatement query = connection.prepareStatement(readAllSQL);
             final ResultSet results = query.executeQuery();
             while (results.next()) {
@@ -413,14 +413,14 @@ public final class JDBCStorageService extends AbstractStorageService implements
                 final Long expires = getExpires(results, 3);
                 final String value = results.getString(4);
                 final Long version = results.getLong(5);
-                log.trace("Record: Context = {}, Id = {}, value = {}, verion = {}, expires = {}",
+                log.trace("Record: Context = '{}', Id = '{}', value = '{}', verion = '{}', expires = '{}'",
                         context, id, value, version, expires == null ? "<never>": expires);
                 result.add(new JDBCStorageRecord<>(value, expires, version));
             }
             return result;
             
         } catch (final SQLException e) {
-            log.error("ReadAll()", e);
+            log.error("ReadAll failed", e);
             throw new IOException(e);
         }
     }
@@ -436,8 +436,10 @@ public final class JDBCStorageService extends AbstractStorageService implements
     @Nonnull @NonnullElements protected List<?> readAll(@Nonnull @NotEmpty final String context)
             throws IOException {
         final List<JDBCStorageRecord<?>> result = new ArrayList<>();
-        log.trace("Getting all Records for context {}", context);
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "ReadAll(String): context must not be null"),
+                             "ReadAll(String): context must not be empty");
         try (final Connection connection = getConnection(true)) {
+            log.trace("ReadAll:: '{}' 1: '{}'  ", readAllSQL, context);
             final PreparedStatement query = connection.prepareStatement(readAllByContextSQL);
             query.setString(1, context);
             final ResultSet results = query.executeQuery();
@@ -446,7 +448,7 @@ public final class JDBCStorageService extends AbstractStorageService implements
                 final Long expires = getExpires(results, 2);
                 final String value = results.getString(3);
                 final Long version = results.getLong(4);
-                log.trace("Record: Id = {}, value = {}, verion = {}, expires = {}",
+                log.trace("Record: Id = '{}', value = '{}', verion = '{}', expires = '{}'",
                         id, value, version, expires == null ? "<never>": expires);
                 result.add(new JDBCStorageRecord<>(value, expires, version));
             }
@@ -462,12 +464,14 @@ public final class JDBCStorageService extends AbstractStorageService implements
  // Checkstyle: CyclomaticComplexity OFF
     public boolean create(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key,
             @Nonnull @NotEmpty final String value, @Nullable @Positive final Long expiration) throws IOException {
-        //
-        // Constraints, Logging
-        //
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "create: context must not be null"),
+                "create: context must not be empty");
+        Constraint.isNotEmpty(Constraint.isNotNull(value, "create: value must not be null"),
+                "create: value must not be empty");
         int retries = transactionRetry;
         while(true) {
             try (final Connection connection = getConnection(false)) {
+                log.trace("Create [Query]:: '{}'::  1: '{}' ; 2: '{}'", preCreateQuerySQL, context, key);
                 // Does it exist?
                 // If not insert
                 // If so check expiration
@@ -476,9 +480,10 @@ public final class JDBCStorageService extends AbstractStorageService implements
                 final PreparedStatement query = connection.prepareStatement(preCreateQuerySQL);
                 query.setString(1, context);
                 query.setString(2, key);
-                log.debug("Querying {}", query);
                 final ResultSet resultSet = query.executeQuery();
                 if (!resultSet.next()) {
+                    log.trace("Create [Insert]:: '{}'  1: '{}' ; 2: '{}' ; 3 '{}' ; 4 '{}'", createCreateRecordSQL, 
+                            context, key, expiration, value);
                     final PreparedStatement insert = connection.prepareStatement(createCreateRecordSQL);
                     insert.setString(1, context);
                     insert.setString(2, key);
@@ -493,7 +498,9 @@ public final class JDBCStorageService extends AbstractStorageService implements
                     log.debug("Duplicate record '{}' in context '{}'", key, context);
                     return false;
                 }
-                final PreparedStatement update = connection.prepareStatement(createCreateRecordSQL);
+                final PreparedStatement update = connection.prepareStatement(createUpdateRecordSQL);
+                log.trace("Create [Update]:: '{}'  1: '{}' ; 2: '{}' ; 3 '{}' ; 4 '{}'", createUpdateRecordSQL, 
+                        value, expiration, context, key);
                 update.setString(1, value);
                 setExpires(update, 2, expiration);
                 update.setString(3,context);
@@ -510,7 +517,6 @@ public final class JDBCStorageService extends AbstractStorageService implements
                         break;
                     }
                 }
-                
                 if (retry) {
                     if (--retries < 0) {
                         log.warn("Error retryable, but retry limit exceeded");
@@ -551,17 +557,20 @@ public final class JDBCStorageService extends AbstractStorageService implements
      */
     // Checkstyle: CyclomaticComplexity OFF
     @Nonnull protected <T> Pair<Long, StorageRecord<T>> readImpl(@Nonnull @NotEmpty final String context,
-            @Nonnull @NotEmpty final String key, @Positive final Long version) throws IOException {
-        //
-        // Constraints, Logging
-        //
+            @Nonnull @NotEmpty final String key, @Positive @Nullable final Long version) throws IOException {
+
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "read: context must not be null"),
+                "read: context must not be empty");
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "read: key must not be null"),
+                "read: key must not be empty");
+
         int retries = transactionRetry;
         while(true) {
             try (final Connection connection = getConnection(true)) {
                 final PreparedStatement stmnt = connection.prepareStatement(readRecordSQL);
+                log.trace("Read:: '{}' 1: '{}' ; 2: '{}'", readRecordSQL, context, key);
                 stmnt.setString(1, context);
                 stmnt.setString(2, key);
-                log.debug("Querying {}", stmnt);
                 final ResultSet resultSet = stmnt.executeQuery();
                 if (!resultSet.next()) {
                     log.debug("Nothing returned");
@@ -570,7 +579,7 @@ public final class JDBCStorageService extends AbstractStorageService implements
                 final Long returnedVersion = resultSet.getLong(1);
                 final Long returnedExpires = getExpires(resultSet, 2);
                 final String returnedValue = resultSet.getString(3);
-                log.debug("Considering Version {}, Expires {}, Value {}",
+                log.trace("Considering Version '{}', Expires '{}', Value '{}'",
                         returnedVersion, returnedValue, returnedExpires);
                 if (returnedExpires != null && System.currentTimeMillis() >= returnedExpires) {
                     log.debug("Read failed, key '{}' expired in context '{}'", key, context);
@@ -656,16 +665,21 @@ public final class JDBCStorageService extends AbstractStorageService implements
             @Nonnull @NotEmpty final String key, @Nonnull @NotEmpty final String value,
             @Nullable @Positive final Long expires) throws IOException, VersionMismatchException {
         
-        //
-        // Constraints, Logging
-        //        
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "update: context must not be null"),
+                "update: context must not be empty");
+        Constraint.isNotEmpty(Constraint.isNotNull(key, "update: key must not be null"),
+                "update: key must not be empty");
+        Constraint.isNotEmpty(Constraint.isNotNull(value, "update: value must not be null"),
+                "update: value must not be empty");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(false)) {
                 final PreparedStatement selectStmnt = connection.prepareStatement(preUpdateQuerySQL);
+                log.trace("Update [Query]:: '{}'  1: '{}' ; 2: '{}'", preUpdateQuerySQL, context, key);
+
                 selectStmnt.setString(1, context);
                 selectStmnt.setString(2, key);
-                log.debug("Querying {}", selectStmnt);
+
                 final ResultSet resultSet = selectStmnt.executeQuery();
                 if (!resultSet.next()) {
                     log.debug("Nothing returned");
@@ -683,8 +697,10 @@ public final class JDBCStorageService extends AbstractStorageService implements
                     throw new VersionMismatchException();
                 }
                 final PreparedStatement updateStmnt = connection.prepareStatement(updateRecordSQL);
-                updateStmnt.setString(1, value);
                 final Long newVersion = Long.valueOf(returnedVersion + 1);
+                log.trace("Update [Update]:: '{}':  1: '{}' ; 2: '{}' ; 3: '{}' ; 4: '{}' ; 5: '{}'", updateRecordSQL,
+                        value, newVersion, expires, context, key);
+                updateStmnt.setString(1, value);
                 updateStmnt.setLong(2, newVersion);
                 setExpires(updateStmnt, 3, expires);
                 updateStmnt.setString(4, context);
@@ -746,16 +762,18 @@ public final class JDBCStorageService extends AbstractStorageService implements
     // Checkstyle: CyclomaticComplexity OFF
     protected boolean deleteImpl(@Nullable @Positive final Long version, @Nonnull @NotEmpty final String context,
             @Nonnull @NotEmpty final String key) throws IOException, VersionMismatchException {
-        //
-        // Constraints, Logging
-        //        
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "delete: context must not be null"),
+                "delete: context must not be empty");
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "delete: key must not be null"),
+                "delete: key must not be empty");
+        Constraint.isTrue(version == null || version > 0, "delete: version should be null of > 0");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(false)) {
                 final PreparedStatement selectStmnt = connection.prepareStatement(preDeleteQuerySQL);
                 selectStmnt.setString(1, context);
                 selectStmnt.setString(2, key);
-                log.debug("Querying {}", selectStmnt);
+                log.trace("Delete [Query]:: '{}':  1: '{}' ; 2: '{}'", preDeleteQuerySQL, context, key);
                 final ResultSet resultSet = selectStmnt.executeQuery();
                 if (!resultSet.next()) {
                     log.debug("Nothing returned");
@@ -766,6 +784,7 @@ public final class JDBCStorageService extends AbstractStorageService implements
                     throw new VersionMismatchException();
                 }
                 final PreparedStatement deleteStmnt = connection.prepareStatement(deleteRecordSQL);
+                log.trace("Delete [Delete]:: '{}':  1: '{}' ; 2: '{}'", deleteRecordSQL, context, key);
                 deleteStmnt.setString(1, context);
                 deleteStmnt.setString(2, key);
                 deleteStmnt.execute();
@@ -803,14 +822,13 @@ public final class JDBCStorageService extends AbstractStorageService implements
      * @throws IOException if errors occur in the cleanup process
      */
     protected void deleteImpl(@Nonnull final Long expiration) throws IOException {
-        //
-        // Constraints, Logging
-        //        
+        Constraint.isNotNull(expiration, "expiration: context must not be null");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(false)) {
                 final PreparedStatement updateStmnt = connection.prepareStatement(deleteByExpiredSQL);
                 updateStmnt.setLong(1, expiration);
+                log.trace("DeleteByExpired:: '{}':  1: '{}' ;", deleteByExpiredSQL, expiration);
                 updateStmnt.execute();
                 connection.commit();
                 return;
@@ -839,15 +857,16 @@ public final class JDBCStorageService extends AbstractStorageService implements
 
     /** {@inheritDoc} */
     public void reap(@Nonnull @NotEmpty final String context) throws IOException {
-        //
-        // Constraints, Logging
-        //
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "reap: context must not be null"),
+                "reap: context must not be empty");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(true)) {
                 final PreparedStatement updateStmnt = connection.prepareStatement(deleteByContextExpiredSQL);
                 updateStmnt.setString(1, context);
-                setExpires(updateStmnt, 2, System.currentTimeMillis());
+                final Long expires = System.currentTimeMillis();
+                setExpires(updateStmnt, 2, expires);
+                log.trace("Reap:: '{}':  1: '{}' ; 2: '{}' ;", deleteByContextExpiredSQL, context, expires);
                 updateStmnt.execute();
                 connection.commit();
                 return;
@@ -876,16 +895,18 @@ public final class JDBCStorageService extends AbstractStorageService implements
     /** {@inheritDoc} */
     public void updateContextExpiration(@Nonnull @NotEmpty final String context, @Nullable final Long  expires)
             throws IOException {
-        //
-        // Constraints, Logging
-        //
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "updateContextExpiration: context must not be null"),
+                "updateContextExpiration: context must not be empty");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(true)) {
                 final PreparedStatement updateStmnt = connection.prepareStatement(updateExpiresByContextSQL);
                 setExpires(updateStmnt, 1, expires);
                 updateStmnt.setString(2, context);
-                setExpires(updateStmnt, 3, System.currentTimeMillis());
+                final Long newExpires = System.currentTimeMillis();
+                setExpires(updateStmnt, 3, newExpires);
+                log.trace("UpdateContextExpiration:: '{}':  1: '{}' ; 2: '{}' ; 3: '{}' ;",
+                        updateExpiresByContextSQL, expires, context, newExpires);
                 updateStmnt.execute();
                 connection.commit();
                 return;
@@ -913,14 +934,15 @@ public final class JDBCStorageService extends AbstractStorageService implements
 
     /** {@inheritDoc} */
     public void deleteContext(@Nonnull @NotEmpty final String context) throws IOException {
-        //
-        // Constraints, Logging
-        //        
+        Constraint.isNotEmpty(Constraint.isNotNull(context, "deleteContext: context must not be null"),
+                "deleteContext: context must not be empty");
         int retries = transactionRetry;
         while (true) {
             try (Connection connection = getConnection(true)) {
                 final PreparedStatement updateStmnt = connection.prepareStatement(deleteByContextSQL);
                 updateStmnt.setString(1, context);
+                log.trace("UpdateContextExpiration:: '{}':  1: '{}'", deleteByContextSQL, context);
+
                 updateStmnt.execute();
                 connection.commit();
                 return;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list