Revision: 5423
http://datanucleus.svn.sourceforge.net/datanucleus/?rev=5423&view=rev
Author: andy_jefferson
Date: 2009-03-06 07:42:05 +0000 (Fri, 06 Mar 2009)
Log Message:
-----------
updated test
Modified Paths:
--------------
test/accessplatform/trunk/test.jdo.jta/src/java/org/datanucleus/tests/jta/GeneralTest.java
Modified: test/accessplatform/trunk/test.jdo.jta/src/java/org/datanucleus/tests/jta/GeneralTest.java
===================================================================
--- test/accessplatform/trunk/test.jdo.jta/src/java/org/datanucleus/tests/jta/GeneralTest.java 2009-03-05 20:52:11 UTC (rev 5422)
+++ test/accessplatform/trunk/test.jdo.jta/src/java/org/datanucleus/tests/jta/GeneralTest.java 2009-03-06 07:42:05 UTC (rev 5423)
@@ -1,665 +1,700 @@
-/**********************************************************************
-Copyright (c) 2007 Guido Anzuoni and others. All rights reserved.
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-
-Contributors:
- ...
-**********************************************************************/
-package org.datanucleus.tests.jta;
-
-import java.util.Collection;
-import java.util.Properties;
-
-import javax.jdo.PersistenceManager;
-import javax.jdo.PersistenceManagerFactory;
-import javax.jdo.Query;
-import javax.jdo.Transaction;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.NotSupportedException;
-import javax.transaction.RollbackException;
-import javax.transaction.Status;
-import javax.transaction.SystemException;
-import javax.transaction.UserTransaction;
-
-import junit.framework.Assert;
-
-import org.datanucleus.tests.JDOPersistenceTestCase;
-import org.datanucleus.tests.TestHelper;
-import org.datanucleus.tests.jta.util.PersistenceManagerDisposer;
-import org.jpox.samples.models.company.Account;
-import org.jpox.samples.one_one.unidir.Login;
-import org.jpox.samples.one_one.unidir.LoginAccount;
-
-/**
- * Series of general tests for JTA.
- * Use of JTA here assumes 2 basic situations
- * <ul>
- * <li>UserTransaction obtained by the user and transactions demarcated using that by the user
- * **before** the JDO transaction is touched. In this case you don't use the JDO txn methods.</li>
- * <li>UserTransaction not obtained, and instead the user demarcates their transaction using the
- * JDO transaction methods. This should automatically start the UserTransaction, and commit it
- * when jdoTx.commit is called.</li>
- * </ul>
- */
-public class GeneralTest extends JDOPersistenceTestCase
-{
- public GeneralTest(String name)
- {
- super(name);
- }
-
- protected void tearDown() throws Exception
- {
- PersistenceManagerFactory lpmf = TestHelper.getPMF(2, null);
- clean(lpmf, Account.class);
- super.tearDown();
- }
-
- public void testEmptyJTA() throws Exception
- {
- // What does this test ?
- }
-
- /**
- * Test that uses JTA but starts the transaction using the JDO transaction methods.
- * See JDO2 16.1.3 for a brief description of the type of situation
- */
- public void testBasicJTAViaJDOTransaction() throws Exception
- {
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(true);
- assertEquals("Transaction type is not JTA!", "JTA", pmf.getTransactionType());
-
- int totals = 0;
- try
- {
- // Demarcate the txn using JDO txn
- tx.begin();
- try
- {
- // Try to start the UserTransaction now (should have been started by JDO txn)
- ut.begin();
- fail("Attempted call to UserTransaction.begin after starting JDO txn directly worked!!");
- }
- catch (Exception e)
- {
- // Expected since the JDO txn started the user transaction
- }
- assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- totals = c.size();
- q.closeAll();
- tx.commit();
- }
- catch (Exception e)
- {
- LOG.info(">> Exception thrown ", e);
- fail("Exception thrown during use of JTA via JDO Transaction");
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- tx.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox");
- pm.makePersistent(accnt);
- tx.commit();
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- tx.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- try
- {
- Assert.assertTrue(c.size() == (totals + 1));
- }
- finally
- {
- q.closeAll();
- tx.commit();
- }
- }
- finally
- {
- pm.close();
- }
- }
-
- public void testBasicJTA() throws Exception
- {
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- int totals = 0;
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- totals = c.size();
- q.closeAll();
- ut.commit();
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox");
- pm.makePersistent(accnt);
- ut.commit();
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- try
- {
- Assert.assertTrue(c.size() == (totals + 1));
- }
- finally
- {
- q.closeAll();
- ut.commit();
- }
- }
- finally
- {
- pm.close();
- }
- }
-
- public void testMultiTxnJTA() throws Exception
- {
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- try
- {
- Assert.assertEquals("should have no elements in db",0,c.size());
- }
- finally
- {
- q.closeAll();
- ut.commit();
- }
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox");
- pm.makePersistent(accnt);
- ut.commit();
- }
- finally
- {
- pm.close();
- }
- //db has now 1 element
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox2");
- pm.makePersistent(accnt);
- pm.flush();
- ut.rollback();
- }
- finally
- {
- pm.close();
- }
- //db should still have 1 element
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- try
- {
- Assert.assertEquals(1,c.size());
- }
- finally
- {
- q.closeAll();
- ut.commit();
- }
- }
- finally
- {
- pm.close();
- }
- }
-
- public void testDelayedFlushExceptionJTA() throws Exception
- {
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- int totals = 0;
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- totals = c.size();
- q.closeAll();
- ut.commit();
- }
- finally
- {
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- boolean rbk = false;
- try
- {
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername(null);
- pm.makePersistent(accnt);
- ut.commit();
- }
- catch (RollbackException ex)
- {
- rbk = true;
- }
- finally
- {
- Assert.assertTrue("Not Rolledback", rbk);
- pm.close();
- }
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(true);
- try
- {
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- try
- {
- Assert.assertTrue(c.size() == (totals));
- }
- finally
- {
- q.closeAll();
- ut.commit();
- }
- }
- finally
- {
- pm.close();
- }
- }
- public void testAddDeleteJTA() throws Exception
- {
- boolean opt = false;
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- c.size();
- q.closeAll();
- ut.commit();
- pm.close();
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox");
- pm.makePersistent(accnt);
- Object oid = pm.getObjectId(accnt);
- ut.commit();
- pm.close();
-
- pm = pmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- accnt = (Account) pm.getObjectById(oid);
- pm.deletePersistent(accnt);
- ut.commit();
- pm.close();
-
- pm = pmf.getPersistenceManager();
- pm.currentTransaction().setOptimistic(opt);
- ut.begin();
- try
- {
- accnt = (Account) pm.getObjectById(oid);
- System.err.println(accnt);
- Assert.assertTrue("accnt still in db:"+pm.getObjectId(accnt), false);
- }
- catch (javax.jdo.JDOObjectNotFoundException ex)
- {
- }
- finally
- {
- ut.commit();
- pm.close();
- }
- }
-
- public void testAddDeleteJTANoBatch() throws Exception
- {
- boolean opt = false;
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(300);
-
- Properties props = new Properties();
- props.put("org.jpox.rdbms.statementBatchLimit", "0");
- PersistenceManagerFactory lpmf = TestHelper.getPMF(1, props);
- PersistenceManager pm = null;
- Transaction tx;
-
- pm = lpmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- Query q = pm.newQuery(Account.class);
- Collection c = (Collection) q.execute();
- c.size();
- q.closeAll();
- ut.commit();
- pm.close();
-
- pm = lpmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- Account accnt = new Account();
- accnt.setUsername("jpox");
- pm.makePersistent(accnt);
- Object oid = pm.getObjectId(accnt);
- ut.commit();
- pm.close();
-
- pm = lpmf.getPersistenceManager();
- tx = pm.currentTransaction();
- tx.setOptimistic(opt);
- ut.begin();
- accnt = (Account) pm.getObjectById(oid);
- pm.deletePersistent(accnt);
- ut.commit();
- pm.close();
-
- pm = lpmf.getPersistenceManager();
- pm.currentTransaction().setOptimistic(opt);
- ut.begin();
- try
- {
- accnt = (Account) pm.getObjectById(oid);
- System.err.println(accnt);
- Assert.assertTrue("accnt still in db:"+pm.getObjectId(accnt), false);
- }
- catch (javax.jdo.JDOObjectNotFoundException ex)
- {
- }
- finally
- {
- ut.commit();
- pm.close();
- }
- }
-
- /**
- * Test that pm.currentTransaction.isActive() is correct when marked for rollback before
- * JTATransactionImpl had a chance to join
- */
- public void testEmptyTxTimeout()
- throws NamingException,
- SystemException,
- NotSupportedException,
- SecurityException,
- IllegalStateException,
- RollbackException,
- HeuristicMixedException,
- HeuristicRollbackException
- {
- try
- {
- PersistenceManager pm = pmf.getPersistenceManager();
- try
- {
- UserTransaction ut = getUserTransaction();
- ut.setTransactionTimeout(1);
- ut.begin();
- synchronized (this)
- {
- // provoke timeout
- try
- {
- wait(1200);
- }
- catch (InterruptedException e)
- {
- throw new RuntimeException(e);
- }
- }
- // make sure we were marked for rollback
- assertTrue(ut.getStatus() == Status.STATUS_MARKED_ROLLBACK);
-
- // by definition should be active even if marked for rollback
- assertTrue(pm.currentTransaction().isActive());
-
- ut.rollback();
- assertFalse(pm.currentTransaction().isActive());
-
- // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
- ut.begin();
- LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
- pm.makePersistent(acct2);
- ut.commit();
- assertFalse(pm.currentTransaction().isActive());
- }
- finally
- {
- // commented out since currently hides the original problem by failing itself
- pm.close();
- }
- }
- finally
- {
- clean(LoginAccount.class);
- }
- }
-
- /**
- * Test expected behaviour upon failed commit with JTA
- * (copied from test.jdo.general/org.datanucleus.tests.TransactionTest.java)
- * @throws NamingException
- * @throws SystemException
- * @throws NotSupportedException
- * @throws HeuristicRollbackException
- * @throws HeuristicMixedException
- * @throws RollbackException
- * @throws IllegalStateException
- * @throws SecurityException
- */
- public void testFailedCommit() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
- {
- try
- {
- PersistenceManager pm = pmf.getPersistenceManager();
- Transaction tx = pm.currentTransaction();
- tx.setOptimistic(true);
- UserTransaction ut = getUserTransaction();
-
- LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
- Login login = acct.getLogin();
- try
- {
- ut.begin();
- pm.makePersistent(acct);
- ut.commit();
-
- // provoke FK violation
- ut.begin();
- pm.deletePersistent(login);
- boolean exceptionCaught = false;
- try
- {
- ut.commit();
- assertTrue("Should have caught exception during commit due to FK violation", false);
- }
- catch (RollbackException e)
- {
- // Expected
- exceptionCaught = true;
- }
- assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
- // receiving a RollbackException in commmit() by definition means the UT was rolled back
- assertFalse(pm.currentTransaction().isActive());
-
- // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
- ut.begin();
- LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
- pm.makePersistent(acct2);
- ut.commit();
- assertFalse(pm.currentTransaction().isActive());
- }
- finally
- {
- if (ut.getStatus()!=Status.STATUS_NO_TRANSACTION)
- {
- ut.rollback();
- }
- pm.close();
- }
- }
- finally
- {
- clean(LoginAccount.class);
- }
- }
-
- public void testCloseOnTxnCompletion()
- throws Exception
- {
- PersistenceManager pm = null;
- pm = pmf.getPersistenceManager();
- new PersistenceManagerDisposer(pm);
- pm.currentTransaction().begin();
- pm.currentTransaction().commit();
- assertTrue("The PersistenceManager is still open", pm.isClosed());
-
- pm = pmf.getPersistenceManager();
- new PersistenceManagerDisposer(pm);
- pm.currentTransaction().begin();
- pm.currentTransaction().rollback();
- assertTrue("The PersistenceManager is still open", pm.isClosed());
-
- UserTransaction ut = getUserTransaction();
- ut.begin();
- pm = pmf.getPersistenceManager();
- new PersistenceManagerDisposer(pm);
- ut.commit();
- assertTrue("The PersistenceManager is still open", pm.isClosed());
-
- ut.begin();
- pm = pmf.getPersistenceManager();
- new PersistenceManagerDisposer(pm);
- ut.rollback();
- assertTrue("The PersistenceManager is still open", pm.isClosed());
- }
-
- private UserTransaction getUserTransaction()
- throws NamingException
- {
- return (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
- }
+/**********************************************************************
+Copyright (c) 2007 Guido Anzuoni and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.tests.jta;
+
+import java.util.Collection;
+import java.util.Properties;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.PersistenceManagerFactory;
+import javax.jdo.Query;
+import javax.jdo.Transaction;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.UserTransaction;
+
+import junit.framework.Assert;
+
+import org.datanucleus.tests.JDOPersistenceTestCase;
+import org.datanucleus.tests.TestHelper;
+import org.datanucleus.tests.jta.util.PersistenceManagerDisposer;
+import org.jpox.samples.models.company.Account;
+import org.jpox.samples.one_one.unidir.Login;
+import org.jpox.samples.one_one.unidir.LoginAccount;
+
+/**
+ * Series of general tests for JTA.
+ * Use of JTA here assumes 2 basic situations
+ * <ul>
+ * <li>UserTransaction obtained by the user and transactions demarcated using that by the user
+ * **before** the JDO transaction is touched. In this case you don't use the JDO txn methods.</li>
+ * <li>UserTransaction not obtained, and instead the user demarcates their transaction using the
+ * JDO transaction methods. This should automatically start the UserTransaction, and commit it
+ * when jdoTx.commit is called.</li>
+ * </ul>
+ */
+public class GeneralTest extends JDOPersistenceTestCase
+{
+ public GeneralTest(String name)
+ {
+ super(name);
+ }
+
+ protected void tearDown() throws Exception
+ {
+ PersistenceManagerFactory lpmf = TestHelper.getPMF(2, null);
+ clean(lpmf, Account.class);
+ super.tearDown();
+ }
+
+ public void testEmptyJTA() throws Exception
+ {
+ // What does this test ?
+ }
+
+ /**
+ * Test that uses JTA but starts the transaction using the JDO transaction methods.
+ * See JDO2 16.1.3 for a brief description of the type of situation
+ */
+ public void testBasicJTAViaJDOTransaction() throws Exception
+ {
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ assertEquals("Transaction type is not JTA!", "JTA", pmf.getTransactionType());
+
+ int totals = 0;
+ try
+ {
+ // Demarcate the txn using JDO txn
+ tx.begin();
+ try
+ {
+ // Try to start the UserTransaction now (should have been started by JDO txn)
+ ut.begin();
+ fail("Attempted call to UserTransaction.begin after starting JDO txn directly worked!!");
+ }
+ catch (Exception e)
+ {
+ // Expected since the JDO txn started the user transaction
+ }
+ assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ totals = c.size();
+ q.closeAll();
+ tx.commit();
+ }
+ catch (Exception e)
+ {
+ LOG.info(">> Exception thrown ", e);
+ fail("Exception thrown during use of JTA via JDO Transaction");
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ tx.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ tx.commit();
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ tx.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ try
+ {
+ Assert.assertTrue(c.size() == (totals + 1));
+ }
+ finally
+ {
+ q.closeAll();
+ tx.commit();
+ }
+ }
+ finally
+ {
+ pm.close();
+ }
+ }
+
+ public void testBasicJTA() throws Exception
+ {
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ int totals = 0;
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ totals = c.size();
+ q.closeAll();
+ ut.commit();
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ ut.commit();
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ try
+ {
+ Assert.assertTrue(c.size() == (totals + 1));
+ }
+ finally
+ {
+ q.closeAll();
+ ut.commit();
+ }
+ }
+ finally
+ {
+ pm.close();
+ }
+ }
+
+ public void testMultiTxnJTA() throws Exception
+ {
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ try
+ {
+ Assert.assertEquals("should have no elements in db",0,c.size());
+ }
+ finally
+ {
+ q.closeAll();
+ ut.commit();
+ }
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ ut.commit();
+ }
+ finally
+ {
+ pm.close();
+ }
+ //db has now 1 element
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox2");
+ pm.makePersistent(accnt);
+ pm.flush();
+ ut.rollback();
+ }
+ finally
+ {
+ pm.close();
+ }
+ //db should still have 1 element
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ try
+ {
+ Assert.assertEquals(1,c.size());
+ }
+ finally
+ {
+ q.closeAll();
+ ut.commit();
+ }
+ }
+ finally
+ {
+ pm.close();
+ }
+ }
+
+ public void testDelayedFlushExceptionJTA() throws Exception
+ {
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ int totals = 0;
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ totals = c.size();
+ q.closeAll();
+ ut.commit();
+ }
+ finally
+ {
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ boolean rbk = false;
+ try
+ {
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername(null);
+ pm.makePersistent(accnt);
+ ut.commit();
+ }
+ catch (RollbackException ex)
+ {
+ rbk = true;
+ }
+ finally
+ {
+ Assert.assertTrue("Not Rolledback", rbk);
+ pm.close();
+ }
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ try
+ {
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ try
+ {
+ Assert.assertTrue(c.size() == (totals));
+ }
+ finally
+ {
+ q.closeAll();
+ ut.commit();
+ }
+ }
+ finally
+ {
+ pm.close();
+ }
+ }
+ public void testAddDeleteJTA() throws Exception
+ {
+ boolean opt = false;
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ c.size();
+ q.closeAll();
+ ut.commit();
+ pm.close();
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ Object oid = pm.getObjectId(accnt);
+ ut.commit();
+ pm.close();
+
+ pm = pmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ accnt = (Account) pm.getObjectById(oid);
+ pm.deletePersistent(accnt);
+ ut.commit();
+ pm.close();
+
+ pm = pmf.getPersistenceManager();
+ pm.currentTransaction().setOptimistic(opt);
+ ut.begin();
+ try
+ {
+ accnt = (Account) pm.getObjectById(oid);
+ System.err.println(accnt);
+ Assert.assertTrue("accnt still in db:"+pm.getObjectId(accnt), false);
+ }
+ catch (javax.jdo.JDOObjectNotFoundException ex)
+ {
+ }
+ finally
+ {
+ ut.commit();
+ pm.close();
+ }
+ }
+
+ public void testAddDeleteJTANoBatch() throws Exception
+ {
+ boolean opt = false;
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(300);
+
+ Properties props = new Properties();
+ props.put("org.jpox.rdbms.statementBatchLimit", "0");
+ PersistenceManagerFactory lpmf = TestHelper.getPMF(1, props);
+ PersistenceManager pm = null;
+ Transaction tx;
+
+ pm = lpmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ Query q = pm.newQuery(Account.class);
+ Collection c = (Collection) q.execute();
+ c.size();
+ q.closeAll();
+ ut.commit();
+ pm.close();
+
+ pm = lpmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ Object oid = pm.getObjectId(accnt);
+ ut.commit();
+ pm.close();
+
+ pm = lpmf.getPersistenceManager();
+ tx = pm.currentTransaction();
+ tx.setOptimistic(opt);
+ ut.begin();
+ accnt = (Account) pm.getObjectById(oid);
+ pm.deletePersistent(accnt);
+ ut.commit();
+ pm.close();
+
+ pm = lpmf.getPersistenceManager();
+ pm.currentTransaction().setOptimistic(opt);
+ ut.begin();
+ try
+ {
+ accnt = (Account) pm.getObjectById(oid);
+ System.err.println(accnt);
+ Assert.assertTrue("accnt still in db:"+pm.getObjectId(accnt), false);
+ }
+ catch (javax.jdo.JDOObjectNotFoundException ex)
+ {
+ }
+ finally
+ {
+ ut.commit();
+ pm.close();
+ }
+ }
+
+ /**
+ * Test that pm.currentTransaction.isActive() is correct when marked for rollback before
+ * JTATransactionImpl had a chance to join
+ */
+ public void testEmptyTxTimeout()
+ throws NamingException,
+ SystemException,
+ NotSupportedException,
+ SecurityException,
+ IllegalStateException,
+ RollbackException,
+ HeuristicMixedException,
+ HeuristicRollbackException
+ {
+ try
+ {
+ PersistenceManager pm = pmf.getPersistenceManager();
+ try
+ {
+ UserTransaction ut = getUserTransaction();
+ ut.setTransactionTimeout(1);
+ ut.begin();
+ synchronized (this)
+ {
+ // provoke timeout
+ try
+ {
+ wait(1200);
+ }
+ catch (InterruptedException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ // make sure we were marked for rollback
+ assertTrue(ut.getStatus() == Status.STATUS_MARKED_ROLLBACK);
+
+ // by definition should be active even if marked for rollback
+ assertTrue(pm.currentTransaction().isActive());
+
+ ut.rollback();
+ assertFalse(pm.currentTransaction().isActive());
+
+ // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
+ ut.begin();
+ LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
+ pm.makePersistent(acct2);
+ ut.commit();
+ assertFalse(pm.currentTransaction().isActive());
+ }
+ finally
+ {
+ // commented out since currently hides the original problem by failing itself
+ pm.close();
+ }
+ }
+ finally
+ {
+ clean(LoginAccount.class);
+ }
+ }
+
+ /**
+ * Test expected behaviour upon failed commit with JTA
+ * (copied from test.jdo.general/org.datanucleus.tests.TransactionTest.java)
+ * @throws NamingException
+ * @throws SystemException
+ * @throws NotSupportedException
+ * @throws HeuristicRollbackException
+ * @throws HeuristicMixedException
+ * @throws RollbackException
+ * @throws IllegalStateException
+ * @throws SecurityException
+ */
+ public void testFailedCommit() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
+ {
+ try
+ {
+ PersistenceManager pm = pmf.getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+ tx.setOptimistic(true);
+ UserTransaction ut = getUserTransaction();
+
+ LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
+ Login login = acct.getLogin();
+ try
+ {
+ ut.begin();
+ pm.makePersistent(acct);
+ ut.commit();
+
+ // provoke FK violation
+ ut.begin();
+ pm.deletePersistent(login);
+ boolean exceptionCaught = false;
+ try
+ {
+ ut.commit();
+ assertTrue("Should have caught exception during commit due to FK violation", false);
+ }
+ catch (RollbackException e)
+ {
+ // Expected
+ exceptionCaught = true;
+ }
+ assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
+ // receiving a RollbackException in commmit() by definition means the UT was rolled back
+ assertFalse(pm.currentTransaction().isActive());
+
+ // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
+ ut.begin();
+ LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
+ pm.makePersistent(acct2);
+ ut.commit();
+ assertFalse(pm.currentTransaction().isActive());
+ }
+ finally
+ {
+ if (ut.getStatus()!=Status.STATUS_NO_TRANSACTION)
+ {
+ ut.rollback();
+ }
+ pm.close();
+ }
+ }
+ finally
+ {
+ clean(LoginAccount.class);
+ }
+ }
+
+ public void testCloseOnTxnCompletion()
+ throws Exception
+ {
+ PersistenceManager pm = null;
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ pm.currentTransaction().begin();
+ Account accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ Object oid = pm.getObjectId(accnt);
+ pm.currentTransaction().commit();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+
+
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ pm.currentTransaction().begin();
+ accnt = (Account) pm.getObjectById(oid);
+ pm.deletePersistent(accnt);
+ pm.currentTransaction().commit();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+
+
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ pm.currentTransaction().begin();
+ accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ pm.currentTransaction().rollback();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+
+ UserTransaction ut = getUserTransaction();
+ ut.begin();
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ oid = pm.getObjectId(accnt);
+ ut.commit();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+
+
+ ut.begin();
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ accnt = (Account) pm.getObjectById(oid);
+ pm.deletePersistent(accnt);
+ ut.commit();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+
+
+ ut.begin();
+ pm = pmf.getPersistenceManager();
+ new PersistenceManagerDisposer(pm);
+ accnt = new Account();
+ accnt.setUsername("jpox");
+ pm.makePersistent(accnt);
+ oid = pm.getObjectId(accnt);
+ ut.rollback();
+ assertTrue("The PersistenceManager is still open", pm.isClosed());
+ }
+
+ private UserTransaction getUserTransaction()
+ throws NamingException
+ {
+ return (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|