Java 在persistence.xml的jta-data-source中放什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4051273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:59:26  来源:igfitidea点击:

What to put into jta-data-source of persistence.xml?

javajpaejbjndiopenejb

提问by yegor256

What value should I place into <jta-data-source>of my persistence.xml?

我应该将什么值放入<jta-data-source>我的persistence.xml?

In glassfish admin panel I created a datasource name "abcDS". In my jndi.properties(inside src/test/resources) I defined it like this:

在 glassfish 管理面板中,我创建了一个数据源名称"abcDS"。在我的jndi.properties(内部src/test/resources)中,我是这样定义的:

[...]
abcDS=new://Resource?type=DataSource
abcDS.JdbcDriver=org.hsqldb.jdbcDriver
abcDS.JdbcUrl=jdbc:hsqldb:mem:testdb
abcDS.JtaManaged=true
[...]

What shall I place into persistence.xml? I've found a lot of variants in the Net, like: "jdbc/abcDS", "java:/abcDS", "abcDS". Which one is right? And is there some rule for this? I understand that it's related to JNDI, but...

我应该放入persistence.xml什么?我在网络中发现了很多变体,例如:"jdbc/abcDS", "java:/abcDS", "abcDS". 哪一个是对的?对此有什么规则吗?我知道它与 JNDI 有关,但是...

I'm trying to create EMF in my unit test:

我正在尝试在我的单元测试中创建 EMF:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("abc");

This is what I'm getting in log:

这是我在日志中得到的:

[...]
SEVERE: Could not find datasource: abcDS javax.naming.NameNotFoundException: 
    Name "abcDS" not found.
at org.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:193)
at org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:150)
at org.apache.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:115)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
[...]

采纳答案by David Blevins

The problem is that Persistence.createEntityManagerFactory("abc")is the "do it yourself" API and doesn't take advantage of the Embedded EJB Container. You can get a container managed EntityManagerin your test case very easily.

问题在于这Persistence.createEntityManagerFactory("abc")是“自己动手”的 API,并没有利用嵌入式 EJB 容器。您可以EntityManager非常轻松地在测试用例中管理容器。

Just as with the related jndi/datasource question I recommend you check out the examples in the examples.zip. They're all designed to take the struggle out of getting started.

与相关的 jndi/datasource 问题一样,我建议您查看 examples.zip 中的示例。它们都旨在消除起步时的困难。

Here's a snippet from the testcase-injectionexample which shows how you can get an EntityManager and other things from the container for use in a test.

这是testcase-injection示例中的一个片段,它展示了如何从容器中获取 EntityManager 和其他东西以用于测试。

First, add an empty ejb-jar.xml or application-client.xml to your test to turn on scanning for your test code:

首先,将一个空的 ejb-jar.xml 或 application-client.xml 添加到您的测试中以打开对您的测试代码的扫描:

  • src/test/resources/META-INF/application-client.xml
  • src/test/resources/META-INF/application-client.xml

Then, annotate your test case with @org.apache.openejb.api.LocalClientand use the standard JavaEE annotations for the actual injection.

然后,@org.apache.openejb.api.LocalClient使用标准 JavaEE 注释对您的测试用例进行注释,并使用实际注入的标准 JavaEE 注释。

@LocalClient
public class MoviesTest extends TestCase {

    @EJB
    private Movies movies;

    @Resource
    private UserTransaction userTransaction;

    @PersistenceContext
    private EntityManager entityManager;

    public void setUp() throws Exception {
        Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
        p.put("movieDatabase", "new://Resource?type=DataSource");
        p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

        InitialContext initialContext = new InitialContext(p);

        // Here's the fun part
        initialContext.bind("inject", this);
    }

As movieDatabaseis the only DataSource that we've setup, OpenEJB will automatically assign that DataSource to your persistence unit without the need to modify your persistence.xml. You can even leave the <jta-data-source>or <non-jta-data-source>empty and OpenEJB will still know what to do.

作为movieDatabase我们设置的唯一数据源,OpenEJB 会自动将该数据源分配给您的持久性单元,而无需修改您的 persistence.xml。您甚至可以将<jta-data-source><non-jta-data-source>留空,OpenEJB 仍然会知道该做什么。

But for the sake of completeness, here's how this particular application has defined the persistence.xml

但为了完整起见,以下是这个特定应用程序如何定义 persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

  <persistence-unit name="movie-unit">
    <jta-data-source>movieDatabase</jta-data-source>
    <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
    <class>org.superbiz.testinjection.Movie</class>

    <properties>
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
  </persistence-unit>
</persistence>

Then the fun part, using it all together in tests

然后是有趣的部分,在测试中一起使用

public void test() throws Exception {

    userTransaction.begin();

    try {
        entityManager.persist(new Movie("Quentin Tarantino", "Reservtheitroad Dogs", 1992));
        entityManager.persist(new Movie("Joel Coen", "Fargo", 1996));
        entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            movies.deleteMovie(movie);
        }

        assertEquals("Movies.getMovies()", 0, movies.getMovies().size());

    } finally {
        userTransaction.commit();
    }
}