You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/03/05 19:59:04 UTC

tomee git commit: fixing equals in ManagedConnection

Repository: tomee
Updated Branches:
  refs/heads/master a71d6ee27 -> 2e6ce35f8


fixing equals in ManagedConnection


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/2e6ce35f
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/2e6ce35f
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/2e6ce35f

Branch: refs/heads/master
Commit: 2e6ce35f890fe8f87355ff31afd9757f7060b2d0
Parents: a71d6ee
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Sat Mar 5 19:58:01 2016 +0100
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Sat Mar 5 19:58:01 2016 +0100

----------------------------------------------------------------------
 .../jdbc/managed/local/ManagedConnection.java   | 31 ++++++++++-----
 .../tomee/jdbc/TomcatXADataSourceTest.java      | 42 ++++++++++++++++++++
 2 files changed, 63 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/2e6ce35f/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java b/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
index 91dfb61..beaa9d3 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
@@ -35,6 +35,7 @@ import javax.transaction.xa.XAResource;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Wrapper;
@@ -77,7 +78,7 @@ public class ManagedConnection implements InvocationHandler {
             return hashCode();
         }
         if ("equals".equals(mtdName)) {
-            return args[0] == this || (delegate != null && delegate.equals(args[0]));
+            return args[0] == this || (delegate != null && delegate.equals(unwrapIfNeeded(args[0])));
         }
 
         // allow to get delegate if needed by the underlying program
@@ -137,15 +138,17 @@ public class ManagedConnection implements InvocationHandler {
 
                     transaction.registerSynchronization(new ClosingSynchronization(xaConnection, delegate));
 
-                    try {
-                        setAutoCommit(false);
-                    } catch (final SQLException xae) { // we are alreay in a transaction so this can't be called from a user perspective - some XA DataSource prevents it in their code
-                        final String message = "Can't set auto commit to false cause the XA datasource doesn't support it, this is likely an issue";
-                        final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RESOURCE_JDBC, ManagedConnection.class);
-                        if (logger.isDebugEnabled()) { // we don't want to print the exception by default
-                            logger.warning(message, xae);
-                        } else {
-                            logger.warning(message);
+                    if (xaConnection == null) {
+                        try {
+                            setAutoCommit(false);
+                        } catch (final SQLException xae) { // we are alreay in a transaction so this can't be called from a user perspective - some XA DataSource prevents it in their code
+                            final String message = "Can't set auto commit to false cause the XA datasource doesn't support it, this is likely an issue";
+                            final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RESOURCE_JDBC, ManagedConnection.class);
+                            if (logger.isDebugEnabled()) { // we don't want to print the exception by default
+                                logger.warning(message, xae);
+                            } else {
+                                logger.warning(message);
+                            }
                         }
                     }
                 } else if (delegate == null) { // shouldn't happen
@@ -165,6 +168,14 @@ public class ManagedConnection implements InvocationHandler {
         }
     }
 
+    private Object unwrapIfNeeded(final Object arg) {
+        if (!Proxy.isProxyClass(arg.getClass())) {
+            return arg;
+        }
+        final InvocationHandler handler = Proxy.getInvocationHandler(arg);
+        return ManagedConnection.class.isInstance(handler) ? ManagedConnection.class.cast(handler).delegate : arg;
+    }
+
     protected Object newConnection() throws SQLException {
         final Object connection = DataSource.class.isInstance(key.ds) ?
                 (key.user == null ? DataSource.class.cast(key.ds).getConnection() : DataSource.class.cast(key.ds).getConnection(key.user, key.pwd)) :

http://git-wip-us.apache.org/repos/asf/tomee/blob/2e6ce35f/tomee/tomee-jdbc/src/test/java/org/apache/tomee/jdbc/TomcatXADataSourceTest.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-jdbc/src/test/java/org/apache/tomee/jdbc/TomcatXADataSourceTest.java b/tomee/tomee-jdbc/src/test/java/org/apache/tomee/jdbc/TomcatXADataSourceTest.java
index 5863384..db72221 100644
--- a/tomee/tomee-jdbc/src/test/java/org/apache/tomee/jdbc/TomcatXADataSourceTest.java
+++ b/tomee/tomee-jdbc/src/test/java/org/apache/tomee/jdbc/TomcatXADataSourceTest.java
@@ -19,6 +19,7 @@ package org.apache.tomee.jdbc;
 import org.apache.openejb.jee.EjbJar;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource;
+import org.apache.openejb.testing.Classes;
 import org.apache.openejb.testing.Configuration;
 import org.apache.openejb.testing.Module;
 import org.apache.openejb.testng.PropertiesBuilder;
@@ -28,6 +29,8 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -39,13 +42,18 @@ import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 @RunWith(ApplicationComposer.class)
 public class TomcatXADataSourceTest {
     @Resource(name = "xadb")
     private DataSource ds;
 
+    @EJB
+    private TxP tx;
+
     @Module
+    @Classes(TxP.class)
     public EjbJar mandatory() {
         return new EjbJar();
     }
@@ -109,5 +117,39 @@ public class TomcatXADataSourceTest {
             assertEquals(0, tds.getActive());
             assertEquals(25, tds.getIdle());
         }
+
+        // in tx
+        for (int it = 0; it < 5; it++) { // ensures it always works and not only the first time
+            for (int i = 0; i < 25; i++) {
+                tx.run(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            Connection c = null;
+                            for (int i = 0; i < 25; i++) {
+                                final Connection connection = ds.getConnection();
+                                connection.getMetaData(); // trigger connection retrieving otherwise nothing is done (pool is not used)
+                                if (c != null) {
+                                    assertEquals(c, connection);
+                                } else {
+                                    c = connection;
+                                }
+                            }
+                        } catch (final SQLException sql) {
+                            fail(sql.getMessage());
+                        }
+                    }
+                });
+            }
+            assertEquals(0, tds.getActive());
+            assertEquals(25, tds.getIdle());
+        }
+    }
+
+    @Singleton
+    public static class TxP {
+        public void run(final Runnable r) {
+            r.run();
+        }
     }
 }