-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #868 from stuartwdouglas/tx-scoped-em
Fixes #867 Make sure then entity manager works when no request scope …
- Loading branch information
Showing
8 changed files
with
165 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...untime/src/main/java/org/jboss/shamrock/jpa/runtime/RequestScopedEntityManagerHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.jboss.shamrock.jpa.runtime; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.annotation.PreDestroy; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
|
||
/** | ||
* Bean that is used to manage request scoped entity managers | ||
*/ | ||
@RequestScoped | ||
public class RequestScopedEntityManagerHolder { | ||
|
||
private final Map<String, EntityManager> entityManagers = new HashMap<>(); | ||
|
||
public EntityManager getOrCreateEntityManager(String name, EntityManagerFactory factory) { | ||
return entityManagers.computeIfAbsent(name, (n) -> factory.createEntityManager()); | ||
} | ||
|
||
@PreDestroy | ||
public void destroy() { | ||
for (Map.Entry<String, EntityManager> entry : entityManagers.entrySet()) { | ||
entry.getValue().close(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
integration-tests/jpa/src/main/java/org/shamrock/jpa/tests/configurationless/Cake.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.shamrock.jpa.tests.configurationless; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import javax.persistence.UniqueConstraint; | ||
|
||
@Entity | ||
@Table(uniqueConstraints = @UniqueConstraint(name = "typeConstraint", columnNames = "type")) | ||
|
||
public class Cake { | ||
private Long id; | ||
private String type; | ||
|
||
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="cakeSeq") | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-tests/jpa/src/main/java/org/shamrock/jpa/tests/configurationless/StartupCakeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.shamrock.jpa.tests.configurationless; | ||
|
||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.event.Observes; | ||
import javax.inject.Inject; | ||
import javax.persistence.EntityManager; | ||
import javax.transaction.Transactional; | ||
|
||
import org.jboss.shamrock.runtime.StartupEvent; | ||
|
||
/** | ||
* creates a chocolate cake on startup to make sure JPA works in the startup event | ||
*/ | ||
@Dependent | ||
public class StartupCakeManager { | ||
|
||
@Inject | ||
EntityManager entityManager; | ||
|
||
@Transactional | ||
public void startup(@Observes StartupEvent startupEvent) { | ||
Cake c = new Cake(); | ||
c.setType("Chocolate"); | ||
entityManager.persist(c); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...on-tests/jpa/src/test/java/org/shamrock/jpa/tests/configurationless/JPAOnStartupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.shamrock.jpa.tests.configurationless; | ||
|
||
import static org.hamcrest.core.StringContains.containsString; | ||
|
||
import org.jboss.shamrock.test.junit.ShamrockTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.restassured.RestAssured; | ||
|
||
/** | ||
* @author Emmanuel Bernard emmanuel@hibernate.org | ||
*/ | ||
@ShamrockTest | ||
public class JPAOnStartupTest { | ||
|
||
@Test | ||
public void testStartupJpa() { | ||
RestAssured.when().get("/jpa-test/cake").then() | ||
.body(containsString("Chocolate")); | ||
} | ||
} |