diff --git a/pom.xml b/pom.xml
index 9a633a12fc3..b2422389e3e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -251,6 +251,21 @@
test
+
+ org.aspectj
+ aspectjrt
+ 1.7.1
+
+
+ org.aspectj
+ aspectjweaver
+ 1.7.1
+
+
+ javax.inject
+ javax.inject
+ 1
+
diff --git a/utils/src/com/cloud/utils/db/TransactionContextBuilder.java b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java
new file mode 100644
index 00000000000..054ddcdd806
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java
@@ -0,0 +1,61 @@
+// 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
+// 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 java.lang.reflect.Method;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.reflect.MethodSignature;
+
+public class TransactionContextBuilder {
+ public TransactionContextBuilder() {
+ }
+
+ public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable {
+ MethodSignature methodSignature = (MethodSignature)call.getSignature();
+ Method targetMethod = methodSignature.getMethod();
+ if(needToIntercept(targetMethod)) {
+ Transaction txn = Transaction.open(call.getSignature().getName());
+ Object ret = null;
+ try {
+ ret = call.proceed();
+ } finally {
+ txn.close();
+ }
+ return ret;
+ }
+ return call.proceed();
+ }
+
+ private boolean needToIntercept(Method method) {
+ DB db = method.getAnnotation(DB.class);
+ if (db != null) {
+ return db.txn();
+ }
+
+ Class> clazz = method.getDeclaringClass();
+ do {
+ db = clazz.getAnnotation(DB.class);
+ if (db != null) {
+ return db.txn();
+ }
+ clazz = clazz.getSuperclass();
+ } while (clazz != Object.class && clazz != null);
+
+ return false;
+ }
+}
diff --git a/utils/test/com/cloud/utils/db/DbAnnotatedBase.java b/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
new file mode 100644
index 00000000000..c9b77b7de41
--- /dev/null
+++ b/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
@@ -0,0 +1,30 @@
+// 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
+// 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 org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+@Component
+@DB
+public class DbAnnotatedBase {
+ private static final Logger s_logger = Logger.getLogger(DbAnnotatedBase.class);
+
+ public void MethodWithClassDbAnnotated() {
+ s_logger.info("called");
+ }
+}
diff --git a/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java b/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java
new file mode 100644
index 00000000000..2b845bc3fc9
--- /dev/null
+++ b/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java
@@ -0,0 +1,27 @@
+// 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
+// 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 org.springframework.stereotype.Component;
+
+@Component
+public class DbAnnotatedBaseDerived {
+
+ @DB
+ public void DbAnnotatedMethod() {
+ }
+}
diff --git a/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java
new file mode 100644
index 00000000000..74e0ab9bcb2
--- /dev/null
+++ b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java
@@ -0,0 +1,25 @@
+package com.cloud.utils.db;
+
+import javax.inject.Inject;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations="classpath:/transactioncontextBuilderTest.xml")
+public class TransactionContextBuilderTest {
+
+ @Inject
+ DbAnnotatedBaseDerived _derived;
+
+ @Inject
+ DbAnnotatedBase _base;
+
+ @Test
+ public void test() {
+ _derived.DbAnnotatedMethod();
+ _base.MethodWithClassDbAnnotated();
+ }
+}
diff --git a/utils/test/resources/transactionContextBuilderTest.xml b/utils/test/resources/transactionContextBuilderTest.xml
new file mode 100644
index 00000000000..4672343a0e2
--- /dev/null
+++ b/utils/test/resources/transactionContextBuilderTest.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+