From commits-return-11965-archive-asf-public=cust-asf.ponee.io@aries.apache.org Mon Jun 14 07:34:57 2021 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mxout1-ec2-va.apache.org (mxout1-ec2-va.apache.org [3.227.148.255]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 81B51180181 for ; Mon, 14 Jun 2021 09:34:57 +0200 (CEST) Received: from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) with SMTP id C0AB33F0F5 for ; Mon, 14 Jun 2021 07:34:29 +0000 (UTC) Received: (qmail 4104 invoked by uid 500); 14 Jun 2021 07:34:29 -0000 Mailing-List: contact commits-help@aries.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@aries.apache.org Delivered-To: mailing list commits@aries.apache.org Received: (qmail 4078 invoked by uid 99); 14 Jun 2021 07:34:29 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jun 2021 07:34:29 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 47E478053C; Mon, 14 Jun 2021 07:34:28 +0000 (UTC) Date: Mon, 14 Jun 2021 07:34:29 +0000 To: "commits@aries.apache.org" Subject: [aries-jpa] 01/01: [ARIES-2050] EMSupplier selectes better TX-related coordination to store EM instance MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: ggrzybek@apache.org In-Reply-To: <162365606876.2008.16565578408744585601@gitbox.apache.org> References: <162365606876.2008.16565578408744585601@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: aries-jpa X-Git-Refname: refs/heads/ARIES-2050 X-Git-Reftype: branch X-Git-Rev: 6fdb8e7e28255b877b1062b391c98df43a8160c6 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20210614073429.47E478053C@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. ggrzybek pushed a commit to branch ARIES-2050 in repository https://gitbox.apache.org/repos/asf/aries-jpa.git commit 6fdb8e7e28255b877b1062b391c98df43a8160c6 Author: Grzegorz Grzybek AuthorDate: Mon Jun 14 09:31:20 2021 +0200 [ARIES-2050] EMSupplier selectes better TX-related coordination to store EM instance --- .../osgi.bnd | 18 +++++ .../jpa-container-blueprint-testbundle-dao/pom.xml | 86 ++++++++++++++++++++++ .../container/itest/bundle/blueprint/CarDao.java | 31 ++++++++ .../itest/bundle/blueprint/NewTxTestService.java | 27 +++++++ .../itest/bundle/blueprint/TestService.java | 25 +++++++ .../itest/bundle/blueprint/dao/CarDaoImpl.java | 45 +++++++++++ .../blueprint/impl/NewTxTestServiceImpl.java | 58 +++++++++++++++ .../bundle/blueprint/impl/TestServiceImpl.java | 72 ++++++++++++++++++ .../main/resources/OSGI-INF/blueprint/config.xml | 48 ++++++++++++ itests/jpa-container-itest/pom.xml | 6 ++ .../blueprint/aries/itest/BlueprintDaoTest.java | 48 ++++++++++++ .../apache/aries/jpa/itest/AbstractJPAItest.java | 4 + itests/pom.xml | 1 + .../aries/jpa/support/impl/EMSupplierImpl.java | 10 +++ 14 files changed, 479 insertions(+) diff --git a/itests/jpa-container-blueprint-testbundle-dao/osgi.bnd b/itests/jpa-container-blueprint-testbundle-dao/osgi.bnd new file mode 100644 index 0000000..13eb520 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/osgi.bnd @@ -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. + +-contract: JavaJPA \ No newline at end of file diff --git a/itests/jpa-container-blueprint-testbundle-dao/pom.xml b/itests/jpa-container-blueprint-testbundle-dao/pom.xml new file mode 100644 index 0000000..d4028d9 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/pom.xml @@ -0,0 +1,86 @@ + + + + + 4.0.0 + + + org.apache.aries.jpa.itest + org.apache.aries.jpa.itest.parent + 2.7.3-SNAPSHOT + ../pom.xml + + + org.apache.aries.jpa.container.itest.bundle.blueprint.dao + bundle + Apache Aries JPA test bundle blueprint integration 2 + Testing blueprint integration with EntityManager, EntityManagerFactory and EmSupplier injection. +Also testing declarative transactions with REQUIRES_NEW behavior + + + true + + + + + org.osgi + org.osgi.compendium + + + org.osgi + org.osgi.core + + + javax.transaction + javax.transaction-api + 1.2 + provided + + + javax.interceptor + javax.interceptor-api + 1.2 + provided + + + org.apache.geronimo.specs + geronimo-jpa_2.0_spec + 1.1 + provided + + + org.apache.aries.jpa.javax.persistence + javax.persistence_2.0 + ${project.version} + provided + + + org.apache.aries.jpa + org.apache.aries.jpa.api + ${project.version} + + + org.apache.aries.jpa + org.apache.aries.jpa.container.itest.bundle + ${project.version} + + + + diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/CarDao.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/CarDao.java new file mode 100644 index 0000000..696a897 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/CarDao.java @@ -0,0 +1,31 @@ +/* + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint; + +import java.util.List; + +import org.apache.aries.jpa.container.itest.entities.Car; + +public interface CarDao { + + void createNew(Car car); + + List getAllCars(); + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/NewTxTestService.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/NewTxTestService.java new file mode 100644 index 0000000..8711257 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/NewTxTestService.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 + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint; + +import javax.transaction.Transaction; + +public interface NewTxTestService { + + void testNewTransaction(Transaction previousTransaction) throws Exception; + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/TestService.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/TestService.java new file mode 100644 index 0000000..4ce7c34 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/TestService.java @@ -0,0 +1,25 @@ +/* + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint; + +public interface TestService { + + void performTest() throws Exception; + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/dao/CarDaoImpl.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/dao/CarDaoImpl.java new file mode 100644 index 0000000..24665c2 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/dao/CarDaoImpl.java @@ -0,0 +1,45 @@ +/* + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint.dao; + +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import org.apache.aries.jpa.container.itest.bundle.blueprint.CarDao; +import org.apache.aries.jpa.container.itest.entities.Car; + +public class CarDaoImpl implements CarDao { + + @PersistenceContext(unitName = "xa-test-unit") + protected EntityManager em; + + @Override + public void createNew(Car car) { + em.persist(car); + } + + @Override + public List getAllCars() { + TypedQuery query = em.createQuery("select c from Car c", Car.class); + return query.getResultList(); + } + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/NewTxTestServiceImpl.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/NewTxTestServiceImpl.java new file mode 100644 index 0000000..fd8e6f1 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/NewTxTestServiceImpl.java @@ -0,0 +1,58 @@ +/* + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint.impl; + +import java.util.List; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; +import javax.transaction.Transactional; +import javax.transaction.Transactional.TxType; + +import org.apache.aries.jpa.container.itest.bundle.blueprint.CarDao; +import org.apache.aries.jpa.container.itest.bundle.blueprint.NewTxTestService; +import org.apache.aries.jpa.container.itest.entities.Car; + +public class NewTxTestServiceImpl implements NewTxTestService { + + private CarDao carDao; + + private TransactionManager transactionManager; + + public void setCarDao(CarDao carDao) { + this.carDao = carDao; + } + + public void setTransactionManager(TransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + + @Override + @Transactional(TxType.REQUIRES_NEW) + public void testNewTransaction(Transaction previousTransaction) throws Exception { + if (previousTransaction == transactionManager.getTransaction()) { + throw new RuntimeException("No new transaction created for TxType.REQUIRES_NEW"); + } + + List cars = carDao.getAllCars(); + if (cars.size() != 0) { + throw new RuntimeException("EntityManager query executed in suspended transaction!"); + } + } + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/TestServiceImpl.java b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/TestServiceImpl.java new file mode 100644 index 0000000..09fea86 --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/TestServiceImpl.java @@ -0,0 +1,72 @@ +/* + * 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 WARRANTIESOR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.jpa.container.itest.bundle.blueprint.impl; + +import java.util.List; +import javax.transaction.TransactionManager; +import javax.transaction.Transactional; +import javax.transaction.Transactional.TxType; + +import org.apache.aries.jpa.container.itest.bundle.blueprint.CarDao; +import org.apache.aries.jpa.container.itest.bundle.blueprint.NewTxTestService; +import org.apache.aries.jpa.container.itest.bundle.blueprint.TestService; +import org.apache.aries.jpa.container.itest.entities.Car; + +public class TestServiceImpl implements TestService { + + private CarDao carDao; + private NewTxTestService newTxTestService; + + private TransactionManager transactionManager; + + public void setCarDao(CarDao carDao) { + this.carDao = carDao; + } + + public void setNewTxTestService(NewTxTestService newTxTestService) { + this.newTxTestService = newTxTestService; + } + + public void setTransactionManager(TransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + + @Override + @Transactional(TxType.REQUIRED) + public void performTest() throws Exception { + try { + List cars = carDao.getAllCars(); + if (cars.size() != 0) { + throw new RuntimeException("Database must be cleaned before executing the test!"); + } + + Car car = new Car(); + car.setNumberOfSeats(4); + car.setEngineSize(42); + car.setNumberPlate("WWW-xxxx"); + car.setColour("blue"); + carDao.createNew(car); + + newTxTestService.testNewTransaction(transactionManager.getTransaction()); + } finally { + transactionManager.setRollbackOnly(); + } + } + +} diff --git a/itests/jpa-container-blueprint-testbundle-dao/src/main/resources/OSGI-INF/blueprint/config.xml b/itests/jpa-container-blueprint-testbundle-dao/src/main/resources/OSGI-INF/blueprint/config.xml new file mode 100644 index 0000000..f0e31cc --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle-dao/src/main/resources/OSGI-INF/blueprint/config.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/itests/jpa-container-itest/pom.xml b/itests/jpa-container-itest/pom.xml index 095956d..3601be3 100644 --- a/itests/jpa-container-itest/pom.xml +++ b/itests/jpa-container-itest/pom.xml @@ -283,6 +283,12 @@ test + org.apache.aries.jpa.itest + org.apache.aries.jpa.container.itest.bundle.blueprint.dao + ${project.version} + test + + org.apache.aries.jpa org.apache.aries.jpa.container.itest.bundle.eclipselink ${project.version} diff --git a/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintDaoTest.java b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintDaoTest.java new file mode 100644 index 0000000..d2d5769 --- /dev/null +++ b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintDaoTest.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 org.apache.aries.jpa.blueprint.aries.itest; + +import org.apache.aries.jpa.container.itest.bundle.blueprint.TestService; +import org.apache.aries.jpa.itest.AbstractJPAItest; +import org.junit.Test; +import org.ops4j.pax.exam.Configuration; +import org.ops4j.pax.exam.Option; + +import static org.ops4j.pax.exam.CoreOptions.mavenBundle; + +public class BlueprintDaoTest extends AbstractJPAItest { + + @Test + public void testNestedTransactionsWithIsolatedEntityManagers() throws Exception { + TestService testService = getService(TestService.class, null); + testService.performTest(); + } + + @Configuration + public Option[] configuration() { + return new Option[] { + baseOptions(), // + jta12Bundles(), // + ariesJpa20(), // + hibernate(), // + derbyDSF(), // + testBundle(), // + testBundleBlueprintDao(), + mavenBundle("org.apache.aries.transaction", "org.apache.aries.transaction.blueprint", "2.3.0-SNAPSHOT"), +// debug() + }; + } +} diff --git a/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java index c0a2692..8d636a6 100644 --- a/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java +++ b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java @@ -327,6 +327,10 @@ public abstract class AbstractJPAItest { return mvnBundle("org.apache.aries.jpa.itest", "org.apache.aries.jpa.container.itest.bundle.blueprint"); } + protected Option testBundleBlueprintDao() { + return mvnBundle("org.apache.aries.jpa.itest", "org.apache.aries.jpa.container.itest.bundle.blueprint.dao"); + } + protected MavenArtifactProvisionOption testBundleEclipseLink() { return mvnBundle("org.apache.aries.jpa", "org.apache.aries.jpa.container.itest.bundle.eclipselink"); } diff --git a/itests/pom.xml b/itests/pom.xml index b1ea0f7..ca76042 100644 --- a/itests/pom.xml +++ b/itests/pom.xml @@ -50,6 +50,7 @@ jpa-container-blueprint-testbundle + jpa-container-blueprint-testbundle-dao jpa-container-itest jpa-container-itest-karaf jpa-container-testbundle diff --git a/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java b/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java index e338659..199de93 100644 --- a/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java +++ b/jpa-support/src/main/java/org/apache/aries/jpa/support/impl/EMSupplierImpl.java @@ -128,9 +128,19 @@ public class EMSupplierImpl implements EmSupplier { } Coordination getTopCoordination() { + // In normal situation, There should be TxInterceptor and JpaInterceptor Coordination coordination = coordinator.peek(); while (coordination != null && coordination.getEnclosingCoordination() != null) { coordination = coordination.getEnclosingCoordination(); + if (coordination != null) { + String transaction = (String) coordination.getVariables().get(Transaction.class); + if (transaction != null) { + // ARIES-2050: transaction-blueprint doesn't always create new coordination (for example + // when calling REQUIRED -> REQUIRED annotated methods. And we can assume the "new" behavior + // when the Transaction.class attribute is not null (it matches one of the 6 TX behaviors) + return coordination; + } + } } return coordination; }