killbill-memoizeit

Details

diff --git a/payment/src/main/resources/org/killbill/billing/payment/dao/TransactionSqlDao.sql.stg b/payment/src/main/resources/org/killbill/billing/payment/dao/TransactionSqlDao.sql.stg
index f75fbba..932a405 100644
--- a/payment/src/main/resources/org/killbill/billing/payment/dao/TransactionSqlDao.sql.stg
+++ b/payment/src/main/resources/org/killbill/billing/payment/dao/TransactionSqlDao.sql.stg
@@ -98,6 +98,5 @@ set transaction_status = :newTransactionStatus
 , updated_by = :updatedBy
 , updated_date = :createdDate
 where <idField("")> in (<ids: {id | :id_<i0>}; separator="," >)
-<defaultOrderBy()>
 ;
->>
\ No newline at end of file
+>>
diff --git a/payment/src/test/java/org/killbill/billing/payment/core/TestPaymentProcessor.java b/payment/src/test/java/org/killbill/billing/payment/core/TestPaymentProcessor.java
index ca3fbd5..72541ec 100644
--- a/payment/src/test/java/org/killbill/billing/payment/core/TestPaymentProcessor.java
+++ b/payment/src/test/java/org/killbill/billing/payment/core/TestPaymentProcessor.java
@@ -177,7 +177,11 @@ public class TestPaymentProcessor extends PaymentTestSuiteWithEmbeddedDB {
                                      final BigDecimal authAmount, final BigDecimal capturedAmount, final BigDecimal refundedAmount,
                                      final int transactionsSize) {
         Assert.assertEquals(payment.getAccountId(), account.getId());
-        Assert.assertEquals(payment.getPaymentNumber(), new Integer(1));
+        // We cannot assume the number to be 1 here as the auto_increment implementation
+        // depends on the database. On h2, it is implemented as a sequence, and the payment number
+        // would be 33, 34, 35, etc. depending on the test
+        // See also http://h2database.com/html/grammar.html#create_sequence
+        Assert.assertTrue(payment.getPaymentNumber() > 0);
         Assert.assertEquals(payment.getExternalKey(), paymentExternalKey);
         Assert.assertEquals(payment.getAuthAmount().compareTo(authAmount), 0);
         Assert.assertEquals(payment.getCapturedAmount().compareTo(capturedAmount), 0);
diff --git a/util/src/main/java/org/killbill/billing/util/dao/LowerToCamelBeanMapper.java b/util/src/main/java/org/killbill/billing/util/dao/LowerToCamelBeanMapper.java
index 6616de8..e4079cd 100644
--- a/util/src/main/java/org/killbill/billing/util/dao/LowerToCamelBeanMapper.java
+++ b/util/src/main/java/org/killbill/billing/util/dao/LowerToCamelBeanMapper.java
@@ -24,6 +24,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
+import java.sql.Blob;
 import java.sql.Date;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
@@ -56,7 +57,7 @@ public class LowerToCamelBeanMapper<T> implements ResultSetMapper<T> {
             for (final PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
                 properties.put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, descriptor.getName()).toLowerCase(), descriptor);
             }
-        } catch (IntrospectionException e) {
+        } catch (final IntrospectionException e) {
             throw new IllegalArgumentException(e);
         }
     }
@@ -65,7 +66,7 @@ public class LowerToCamelBeanMapper<T> implements ResultSetMapper<T> {
         final T bean;
         try {
             bean = type.newInstance();
-        } catch (Exception e) {
+        } catch (final Exception e) {
             throw new IllegalArgumentException(String.format("A bean, %s, was mapped " +
                                                              "which was not instantiable", type.getName()),
                                                e);
@@ -124,6 +125,12 @@ public class LowerToCamelBeanMapper<T> implements ResultSetMapper<T> {
                     value = rs.getObject(i);
                 }
 
+                // For h2, transform a JdbcBlob into a byte[]
+                if (value instanceof Blob) {
+                    final Blob blob = (Blob) value;
+                    value = blob.getBytes(0, (int) blob.length());
+                }
+
                 if (rs.wasNull() && !type.isPrimitive()) {
                     value = null;
                 }
@@ -138,16 +145,16 @@ public class LowerToCamelBeanMapper<T> implements ResultSetMapper<T> {
                         field.setAccessible(true); // Often private...
                         field.set(bean, value);
                     }
-                } catch (NoSuchFieldException e) {
+                } catch (final NoSuchFieldException e) {
                     throw new IllegalArgumentException(String.format("Unable to find field for " +
                                                                      "property, %s", name), e);
-                } catch (IllegalAccessException e) {
+                } catch (final IllegalAccessException e) {
                     throw new IllegalArgumentException(String.format("Unable to access setter for " +
                                                                      "property, %s", name), e);
-                } catch (InvocationTargetException e) {
+                } catch (final InvocationTargetException e) {
                     throw new IllegalArgumentException(String.format("Invocation target exception trying to " +
                                                                      "invoker setter for the %s property", name), e);
-                } catch (NullPointerException e) {
+                } catch (final NullPointerException e) {
                     throw new IllegalArgumentException(String.format("No appropriate method to " +
                                                                      "write value %s ", value.toString()), e);
                 }
@@ -160,7 +167,7 @@ public class LowerToCamelBeanMapper<T> implements ResultSetMapper<T> {
     private static Field getField(final Class clazz, final String fieldName) throws NoSuchFieldException {
         try {
             return clazz.getDeclaredField(fieldName);
-        } catch (NoSuchFieldException e) {
+        } catch (final NoSuchFieldException e) {
             // Go up in the hierarchy
             final Class superClass = clazz.getSuperclass();
             if (superClass == null) {
diff --git a/util/src/main/java/org/killbill/billing/util/glue/EhCacheManagerProvider.java b/util/src/main/java/org/killbill/billing/util/glue/EhCacheManagerProvider.java
index bd742f8..3577271 100644
--- a/util/src/main/java/org/killbill/billing/util/glue/EhCacheManagerProvider.java
+++ b/util/src/main/java/org/killbill/billing/util/glue/EhCacheManagerProvider.java
@@ -22,6 +22,7 @@ import javax.inject.Provider;
 import org.apache.shiro.cache.ehcache.EhCacheManager;
 import org.apache.shiro.mgt.DefaultSecurityManager;
 import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
 
 import net.sf.ehcache.CacheManager;
 
@@ -42,6 +43,11 @@ public class EhCacheManagerProvider implements Provider<EhCacheManager> {
         // Same EhCache manager instance as the rest of the system
         shiroEhCacheManager.setCacheManager(ehCacheCacheManager);
 
+        // It looks like Shiro's cache manager is not thread safe. Concurrent requests on startup
+        // can throw org.apache.shiro.cache.CacheException: net.sf.ehcache.ObjectExistsException: Cache shiro-activeSessionCache already exists
+        // As a workaround, create the cache manually here
+        shiroEhCacheManager.getCache(CachingSessionDAO.ACTIVE_SESSION_CACHE_NAME);
+
         if (securityManager instanceof DefaultSecurityManager) {
             ((DefaultSecurityManager) securityManager).setCacheManager(shiroEhCacheManager);
         }