openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r491147 - in /incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel: AbstractJDBCSeq.java NativeJDBCSeq.java TableJDBCSeq.java
Date Sat, 30 Dec 2006 01:20:56 GMT
Author: pcl
Date: Fri Dec 29 17:20:55 2006
New Revision: 491147

URL: http://svn.apache.org/viewvc?view=rev&rev=491147
Log:
Code changes for OPENJPA-93: sequence maintainance in JTA env without non-JTA datasource.
This has been nominally tested in WebLogic Server.

Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java?view=diff&rev=491147&r1=491146&r2=491147
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
Fri Dec 29 17:20:55 2006
@@ -18,6 +18,8 @@
 import java.sql.Connection;
 import java.sql.SQLException;
 import javax.sql.DataSource;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
 
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.jdbc.meta.ClassMapping;
@@ -40,6 +42,7 @@
 
     protected int type = TYPE_DEFAULT;
     protected Object current = null;
+    private transient Transaction _outerTransaction;
 
     /**
      * Records the sequence type.
@@ -106,6 +109,11 @@
     protected abstract Object nextInternal(JDBCStore store,
         ClassMapping mapping)
         throws Exception;
+    
+    /**
+     * Return the {@link JDBCConfiguration} for this sequence.
+     */
+    public abstract JDBCConfiguration getConfiguration();
 
     /**
      * Return the current sequence object. By default returns the last
@@ -140,13 +148,24 @@
         throws SQLException {
         if (type == TYPE_TRANSACTIONAL || type == TYPE_CONTIGUOUS)
             return store.getConnection();
-
-        JDBCConfiguration conf = store.getConfiguration();
-        DataSource ds = conf.getDataSource2(store.getContext());
-        Connection conn = ds.getConnection();
-        if (conn.getAutoCommit())
-            conn.setAutoCommit(false);
-        return conn;
+        else if (suspendInJTA()) {
+            try {
+                TransactionManager tm = getConfiguration()
+                    .getManagedRuntimeInstance().getTransactionManager();
+                _outerTransaction = tm.suspend();
+                tm.begin();
+                return store.getConnection();
+            } catch (Exception e) {
+                throw new StoreException(e);
+            }
+        } else {
+            JDBCConfiguration conf = store.getConfiguration();
+            DataSource ds = conf.getDataSource2(store.getContext());
+            Connection conn = ds.getConnection();
+            if (conn.getAutoCommit())
+                conn.setAutoCommit(false);
+            return conn;
+        }
     }
 
     /**
@@ -156,13 +175,41 @@
         if (conn == null)
             return;
 
-        try {
-            if (type != TYPE_TRANSACTIONAL && type != TYPE_CONTIGUOUS)
+        if (type == TYPE_TRANSACTIONAL || type == TYPE_CONTIGUOUS) {
+            // do nothing; this seq is part of the business transaction
+            return;
+        } else if (suspendInJTA()) {
+            try {
+                TransactionManager tm = getConfiguration()
+                    .getManagedRuntimeInstance().getTransactionManager();
+                tm.commit();
+                try { conn.close(); } catch (SQLException se) {}
+
+                if (_outerTransaction != null)
+                    tm.resume(_outerTransaction);
+
+            } catch (Exception e) {
+                throw new StoreException(e);
+            } finally {
+                _outerTransaction = null;
+            }
+        } else {
+            try {
                 conn.commit();
-        } catch (SQLException se) {
-            throw SQLExceptions.getStore(se);
-        } finally {
-            try { conn.close(); } catch (SQLException se) {}
+            } catch (SQLException se) {
+                throw SQLExceptions.getStore(se);
+            } finally {
+                try { conn.close(); } catch (SQLException se) {}
+            }
         }
+    }
+    
+    /**
+     * Detect whether or not OpenJPA should suspend the transaction in 
+     * a managed environment.
+     */
+    protected boolean suspendInJTA() {
+        return getConfiguration().isConnectionFactoryModeManaged() && 
+            getConfiguration().getConnectionFactory2() == null;
     }
 }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java?view=diff&rev=491147&r1=491146&r2=491147
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
Fri Dec 29 17:20:55 2006
@@ -172,6 +172,10 @@
         schema.importSequence(_seq);
     }
 
+    public JDBCConfiguration getConfiguration() {
+        return _conf;
+    }
+    
     public void setConfiguration(Configuration conf) {
         _conf = (JDBCConfiguration) conf;
     }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java?view=diff&rev=491147&r1=491146&r2=491147
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java
Fri Dec 29 17:20:55 2006
@@ -330,7 +330,8 @@
         try {
             // possible that we might get errors when inserting if
             // another thread/process is inserting same pk at same time
-            SQLException err = null;
+            SQLException err = null; 
+            // ### why does this not call getConnection() / closeConnection()?
             conn = _conf.getDataSource2(store.getContext()).getConnection();
             try {
                 insertSequence(mapping, conn);
@@ -377,7 +378,7 @@
             appendValue(Numbers.valueOf(1), _seqColumn).append(")");
 
         boolean wasAuto = conn.getAutoCommit();
-        if (!wasAuto)
+        if (!wasAuto && !suspendInJTA())
             conn.setAutoCommit(true);
 
         PreparedStatement stmnt = null;
@@ -387,7 +388,7 @@
         } finally {
             if (stmnt != null)
                 try { stmnt.close(); } catch (SQLException se) {}
-            if (!wasAuto)
+            if (!wasAuto && !suspendInJTA())
                 conn.setAutoCommit(false);
         }
     }



Mime
View raw message