Author: rmannibucau
Date: Fri Aug  3 08:12:06 2012
New Revision: 1368829

URL: http://svn.apache.org/viewvc?rev=1368829&view=rev
Log:
isclosed in managedconnection and some checkup in TomEEDataSourceCreator

Added:
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceIsClosedTest.java
      - copied, changed from r1368558, 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceTest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/LocalXAResource.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
    
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/LocalXAResource.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/LocalXAResource.java?rev=1368829&r1=1368828&r2=1368829&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/LocalXAResource.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/LocalXAResource.java
 Fri Aug  3 08:12:06 2012
@@ -85,15 +85,15 @@ public class LocalXAResource implements 
     @Override
     public void end(final Xid xid, int flag) throws XAException {
         try {
-        if (xid == null) {
-            throw new NullPointerException("xid is null");
-        }
-        if (!this.currentXid.equals(xid)) {
-            throw new XAException("Invalid Xid: expected " + this.currentXid + 
", but was " + xid);
-        }
+            if (xid == null) {
+                throw new NullPointerException("xid is null");
+            }
+            if (!this.currentXid.equals(xid)) {
+                throw new XAException("Invalid Xid: expected " + 
this.currentXid + ", but was " + xid);
+            }
         } finally {
             lock.unlock();
-    }
+        }
     }
 
     @Override

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java?rev=1368829&r1=1368828&r2=1368829&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/managed/local/ManagedConnection.java
 Fri Aug  3 08:12:06 2012
@@ -20,18 +20,22 @@ import java.util.concurrent.ConcurrentHa
 public class ManagedConnection implements InvocationHandler {
     private static final Map<Transaction, Connection> CONNECTION_BY_TX = new 
ConcurrentHashMap<Transaction, Connection>();
 
-    protected Connection delegate;
     private final TransactionManager transactionManager;
+    private final LocalXAResource xaResource;
+    protected Connection delegate;
     private Transaction currentTransaction;
+    private boolean closed;
 
 
     public ManagedConnection(final Connection connection, final 
TransactionManager txMgr) {
         delegate = connection;
         transactionManager = txMgr;
+        closed = false;
+        xaResource = new LocalXAResource(delegate);
     }
 
     public XAResource getXAResource() throws SQLException {
-        return new LocalXAResource(delegate);
+        return xaResource;
     }
 
     @Override
@@ -105,7 +109,7 @@ public class ManagedConnection implement
 
     }
 
-    private static Object invokeUnderTransaction(final Connection delegate, 
final Method method, final Object[] args) throws Exception {
+    private Object invokeUnderTransaction(final Connection delegate, final 
Method method, final Object[] args) throws Exception {
         final String mtdName = method.getName();
         if ("setAutoCommit".equals(mtdName)
                 || "commit".equals(mtdName)
@@ -115,15 +119,18 @@ public class ManagedConnection implement
             throw forbiddenCall(mtdName);
         }
         if ("close".equals(mtdName)) {
-            // will be done later
-            // we need to delay it in case of rollback
-            return noop();
+            return close();
+        }
+        if ("isClosed".equals(mtdName) && closed) {
+            return true; // if !closed let's delegate to the underlying 
connection
         }
         return method.invoke(delegate, args);
     }
 
-    // just to give a better semantiv to a trivial operation
-    private static Object noop() {
+    // will be done later
+    // we need to delay it in case of rollback
+    private Object close() {
+        closed = true;
         return null;
     }
 

Copied: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceIsClosedTest.java
 (from r1368558, 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceTest.java)
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceIsClosedTest.java?p2=openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceIsClosedTest.java&p1=openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceTest.java&r1=1368558&r2=1368829&rev=1368829&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceTest.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceIsClosedTest.java
 Fri Aug  3 08:12:06 2012
@@ -16,14 +16,10 @@
  */
 package org.apache.openejb.resource.jdbc;
 
-import org.apache.openejb.jee.EjbJar;
 import org.apache.openejb.jee.SingletonBean;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.junit.Configuration;
 import org.apache.openejb.junit.Module;
-import org.apache.openejb.resource.jdbc.managed.local.ManagedConnection;
-import org.junit.After;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -32,44 +28,22 @@ import javax.ejb.EJB;
 import javax.ejb.EJBContext;
 import javax.ejb.LocalBean;
 import javax.ejb.Singleton;
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
 import javax.sql.DataSource;
-import java.lang.reflect.Field;
 import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Map;
 import java.util.Properties;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 @RunWith(ApplicationComposer.class)
-public class ManagedDataSourceTest {
-    private static final String URL = 
"jdbc:hsqldb:mem:managed;hsqldb.tx=MVCC"; // mvcc otherwise multiple 
transaction tests will fail
+public class ManagedDataSourceIsClosedTest {
+    private static final String URL = "jdbc:hsqldb:mem:isclosed";
     private static final String USER = "sa";
     private static final String PASSWORD = "";
-    private static final String TABLE = "PUBLIC.MANAGED_DATASOURCE_TEST";
 
     @EJB
     private Persister persistManager;
 
-    @BeforeClass
-    public static void createTable() throws SQLException, 
ClassNotFoundException {
-        Class.forName("org.hsqldb.jdbcDriver");
-
-        final Connection connection = DriverManager.getConnection(URL, USER, 
PASSWORD);
-        final Statement statement = connection.createStatement();
-        statement.execute("CREATE TABLE " + TABLE + "(ID INTEGER)");
-        statement.close();
-        connection.commit();
-        connection.close();
-    }
-
     @Configuration
     public Properties config() {
         final Properties p = new Properties();
@@ -85,30 +59,8 @@ public class ManagedDataSourceTest {
     }
 
     @Module
-    public EjbJar app() throws Exception {
-        return new EjbJar()
-                .enterpriseBean(new SingletonBean(Persister.class).localBean())
-                .enterpriseBean(new 
SingletonBean(OtherPersister.class).localBean());
-    }
-
-    @LocalBean
-    @Singleton
-    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
-    public static class OtherPersister {
-        @Resource(name = "managed")
-        private DataSource ds;
-
-        @Resource
-        private EJBContext context;
-
-        public void save() throws SQLException {
-            ManagedDataSourceTest.save(ds, 10);
-        }
-
-        public void saveAndRollback() throws SQLException {
-            ManagedDataSourceTest.save(ds, 11);
-            context.setRollbackOnly();
-        }
+    public SingletonBean app() throws Exception {
+        return (SingletonBean) new SingletonBean(Persister.class).localBean();
     }
 
     @LocalBean
@@ -120,110 +72,15 @@ public class ManagedDataSourceTest {
         @Resource
         private EJBContext context;
 
-        @EJB
-        private OtherPersister other;
-
-        public void save() throws SQLException {
-            ManagedDataSourceTest.save(ds, 1);
-        }
-
-        public void saveAndRollback() throws SQLException {
-            ManagedDataSourceTest.save(ds, 2);
-            context.setRollbackOnly();
-        }
-
-        public void saveTwice() throws SQLException {
-            ManagedDataSourceTest.save(ds, 3);
-            ManagedDataSourceTest.save(ds, 4);
-    }
-
-        public void rollbackMultipleSave() throws SQLException {
-            ManagedDataSourceTest.save(ds, 5);
-            ManagedDataSourceTest.save(ds, 6);
-            context.setRollbackOnly();
-        }
-
-        public void saveInThisTxAndAnotherOne() throws SQLException {
-            ManagedDataSourceTest.save(ds, 7);
-            other.save();
-        }
-
-        public void saveInThisTxAndRollbackInAnotherOne() throws SQLException {
-            ManagedDataSourceTest.save(ds, 8);
-            other.saveAndRollback();
+        public void isClosed() throws SQLException {
+            final Connection c = ds.getConnection();
+            c.close();
+            assertTrue(c.isClosed());
         }
     }
 
     @Test
-    public void commit() throws SQLException {
-        persistManager.save();
-        assertTrue(exists(1));
-    }
-
-    @Test
-    public void rollback() throws SQLException {
-            persistManager.saveAndRollback();
-        assertFalse(exists(2));
-    }
-
-    @Test
-    public void commit2() throws SQLException {
-        persistManager.saveTwice();
-        assertTrue(exists(3));
-        assertTrue(exists(4));
-    }
-
-    @Test
-    public void rollback2() throws SQLException {
-        persistManager.rollbackMultipleSave();
-        assertFalse(exists(5));
-        assertFalse(exists(6));
-    }
-
-    @Test
-    public void saveDifferentTx() throws SQLException {
-        persistManager.saveInThisTxAndAnotherOne();
-        assertTrue(exists(7));
-        assertTrue(exists(10));
-    }
-
-    @Test
-    public void saveRollbackDifferentTx() throws SQLException {
-        persistManager.saveInThisTxAndRollbackInAnotherOne();
-        assertTrue(exists(8));
-        assertFalse(exists(12));
-    }
-
-    @After
-    public void checkTxMapIsEmpty() throws Exception { // avoid memory leak
-        final Field map = 
ManagedConnection.class.getDeclaredField("CONNECTION_BY_TX");
-        map.setAccessible(true);
-        final Map<?, ?> instance = (Map<?, ?>) map.get(null);
-        assertEquals(0, instance.size());
-    }
-
-    private static boolean exists(int id) throws SQLException {
-        final Connection connection = DriverManager.getConnection(URL, USER, 
PASSWORD);
-        final Statement statement = connection.createStatement();
-        final ResultSet result = statement.executeQuery("SELECT count(*) AS NB 
FROM " + TABLE + " WHERE ID = " + id);
-        try {
-            assertTrue(result.next());
-            return result.getInt(1) == 1;
-        } finally {
-            statement.close();
-            connection.close();
-        }
-    }
-
-    private static void save(final DataSource ds, int id) throws SQLException {
-        execute(ds, "INSERT INTO " + TABLE + "(ID) VALUES(" + id + ")");
-    }
-
-    private static void execute(final DataSource ds, final String sql) throws 
SQLException {
-        final Connection connection = ds.getConnection();
-        final Statement statement = connection.createStatement();
-        statement.executeUpdate(sql);
-        statement.close();
-        connection.close();
+    public void isClosed() throws SQLException {
+        persistManager.isClosed();
     }
 }

Modified: 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java?rev=1368829&r1=1368828&r2=1368829&view=diff
==============================================================================
--- 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
 (original)
+++ 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
 Fri Aug  3 08:12:06 2012
@@ -34,7 +34,7 @@ public class TomEEDataSourceCreator exte
         } catch (SQLException e) {
             throw new IllegalStateException(e);
         }
-        return build(TomEEDataSource.class, new TomEEDataSource(pool, name), 
converted);
+        return build(TomEEDataSource.class, new TomEEDataSource(config, pool, 
name), converted);
     }
 
     @Override
@@ -91,7 +91,8 @@ public class TomEEDataSourceCreator exte
     }
 
     public static class TomEEDataSource extends 
org.apache.tomcat.jdbc.pool.DataSource {
-        public TomEEDataSource(final ConnectionPool pool, final String name) {
+        public TomEEDataSource(final PoolConfiguration properties, final 
ConnectionPool pool, final String name) {
+            super(properties);
             this.pool = pool;
             try {
                 preRegister(LocalMBeanServer.get(), new ObjectName("openejb", 
"name", name));
@@ -102,16 +103,12 @@ public class TomEEDataSourceCreator exte
 
         public TomEEDataSource(final PoolConfiguration poolConfiguration, 
final String name) {
             super(poolConfiguration);
-            try { // just to force the pool to be created
-                getConnection().close();
+            try { // just to force the pool to be created and be able to 
register the mbean
+                createPool();
+                preRegister(LocalMBeanServer.get(), new ObjectName("openejb", 
"name", name));
             } catch (Throwable ignored) {
                 // no-op
             }
-            try {
-                preRegister(LocalMBeanServer.get(), new ObjectName("openejb", 
"name", name));
-            } catch (Exception ignored) {
-                // ignored
-            }
         }
 
         @Override


Reply via email to