diff --git a/framework/db/src/com/cloud/utils/db/Transaction.java b/framework/db/src/com/cloud/utils/db/Transaction.java index 4f8e5c7d01d..d65b87e9fbb 100755 --- a/framework/db/src/com/cloud/utils/db/Transaction.java +++ b/framework/db/src/com/cloud/utils/db/Transaction.java @@ -27,7 +27,12 @@ public class Transaction { public static T execute(TransactionCallback callback) { String name = "tx-" + counter.incrementAndGet(); - TransactionLegacy txn = TransactionLegacy.open(name); + short databaseId = TransactionLegacy.CLOUD_DB; + TransactionLegacy currentTxn = TransactionLegacy.currentTxn(false); + if ( currentTxn != null ) { + databaseId = currentTxn.getDatabaseId(); + } + TransactionLegacy txn = TransactionLegacy.open(name, databaseId, false); try { txn.start(); T result = callback.doInTransaction(STATUS); diff --git a/framework/db/src/com/cloud/utils/db/TransactionLegacy.java b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java index b191491b9e7..ffa06707df9 100755 --- a/framework/db/src/com/cloud/utils/db/TransactionLegacy.java +++ b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java @@ -117,10 +117,16 @@ public class TransactionLegacy { private TransactionLegacy _prev = null; public static TransactionLegacy currentTxn() { + return currentTxn(true); + } + + protected static TransactionLegacy currentTxn(boolean check) { TransactionLegacy txn = tls.get(); - assert txn != null : "No Transaction on stack. Did you mark the method with @DB?"; - - assert checkAnnotation(3, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn; + if (check) { + assert txn != null : "No Transaction on stack. Did you mark the method with @DB?"; + + assert checkAnnotation(4, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn; + } return txn; } @@ -397,6 +403,10 @@ public class TransactionLegacy { return lockMaster.release(name); } + /** + * @deprecated Use {@link Transaction} for new code + */ + @Deprecated public void start() { if (s_logger.isTraceEnabled()) { s_logger.trace("txn: start requested by: " + buildName()); @@ -1170,5 +1180,13 @@ public class TransactionLegacy { return new PoolingDataSource( /* connectionPool */poolableConnectionFactory.getPool()); } - + + /** + * Used for unit testing primarily + * + * @param conn + */ + protected void setConnection(Connection conn) { + this._conn = conn; + } } diff --git a/framework/db/test/com/cloud/utils/db/TestTransaction.java b/framework/db/test/com/cloud/utils/db/TestTransaction.java new file mode 100644 index 00000000000..ea277c7b6cb --- /dev/null +++ b/framework/db/test/com/cloud/utils/db/TestTransaction.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package com.cloud.utils.db; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.io.FileNotFoundException; +import java.sql.Connection; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestTransaction { + + TransactionLegacy txn; + Connection conn; + + @Before + public void setup() { + setup(TransactionLegacy.CLOUD_DB); + } + + public void setup(short db) { + txn = TransactionLegacy.open(db); + conn = Mockito.mock(Connection.class); + txn.setConnection(conn); + } + + @After + public void after() { + TransactionLegacy.currentTxn().close(); + } + + @Test + public void testCommit() throws Exception { + assertEquals(42L, Transaction.execute(new TransactionCallback() { + @Override + public Object doInTransaction(TransactionStatus status) { + return 42L; + } + })); + + verify(conn).setAutoCommit(false); + verify(conn, times(1)).commit(); + verify(conn, times(0)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testRollback() throws Exception { + try { + Transaction.execute(new TransactionCallback() { + @Override + public Object doInTransaction(TransactionStatus status) { + throw new RuntimeException("Panic!"); + } + }); + fail(); + } catch (RuntimeException e) { + assertEquals("Panic!", e.getMessage()); + } + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testRollbackWithException() throws Exception { + try { + Transaction.executeWithException(new TransactionCallbackWithException() { + @Override + public Object doInTransaction(TransactionStatus status) throws FileNotFoundException { + assertEquals(TransactionLegacy.CLOUD_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue()); + + throw new FileNotFoundException("Panic!"); + } + }, FileNotFoundException.class); + fail(); + } catch (FileNotFoundException e) { + assertEquals("Panic!", e.getMessage()); + } + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testOtherdatabaseRollback() throws Exception { + after(); + setup(TransactionLegacy.AWSAPI_DB); + + try { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + assertEquals(TransactionLegacy.AWSAPI_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue()); + + throw new RuntimeException("Panic!"); + } + }); + fail(); + } catch (RuntimeException e) { + assertEquals("Panic!", e.getMessage()); + } + + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + +} diff --git a/framework/db/test/db.properties b/framework/db/test/db.properties new file mode 100644 index 00000000000..cc1215ff35c --- /dev/null +++ b/framework/db/test/db.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Just here to make the unit test not blow up \ No newline at end of file diff --git a/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java new file mode 100644 index 00000000000..33fca37692b --- /dev/null +++ b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package com.cloud.utils.exception; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.junit.Test; + +public class ExceptionUtilTest { + + @Test + public void test() throws Exception { + FileNotFoundException fnfe = new FileNotFoundException(); + try { + ExceptionUtil.rethrow(fnfe, IOException.class); + fail(); + } catch (IOException e) { + } + + ExceptionUtil.rethrow(fnfe, ClassNotFoundException.class); + + try { + ExceptionUtil.rethrow(fnfe, FileNotFoundException.class); + fail(); + } catch ( FileNotFoundException e ) { + } + } + +}