持久性文件的jta-data-source的内容是什么? [英] What to put into jta-data-source of persistence.xml?

查看:590
本文介绍了持久性文件的jta-data-source的内容是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在 persistence.xml 中的< jta-data-source> / p>

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

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

我应该在 persistence.xml 中添加什么?我在网络中找到了很多变体,例如:jdbc / abcDSjava:/ abcDSabcDS。哪一个是对的?这有什么规则吗?我知道它与JNDI有关,但... ...



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

  EntityManagerFactory emf = Persistence.createEntityManagerFactory(abc); 

这是我在日志中获取的:

  [...] 
SEVERE:无法找到数据源:abcDS javax.naming.NameNotFoundException:
未找到名称abcDS。
在org.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:193)
在org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext。 java:150)
在org.apache.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:115)
在javax.naming.InitialContext.lookup(InitialContext.java:392)
[...]


解决方案

Persistence.createEntityManagerFactory(abc)是自己动手的API,不利用嵌入式EJB容器。您可以轻松地在测试用例中获取一个容器管理 EntityManager



正如相关的jndi / datasource一样问题我建议您查看示例中的示例.ZIP



以下是 testcase-injection 中的一段代码片段,示例,显示如何从容器中获取EntityManager和其他内容以供测试使用。



首先,添加一个空的ejb-jar.xml或应用程序客户端.xml进行测试以开启扫描测试代码:




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



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

  @LocalClient 
public class MoviesTest扩展TestCase {

@EJB
私人电影电影;

@Resource
private UserTransaction userTransaction;

@PersistenceContext
private EntityManager entityManager;

public void setUp()throws异常{
属性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);

//这是有趣的部分
initialContext.bind(inject,this);
}

As movieDatabase 我们设置的唯一DataSource,OpenEJB将自动将该DataSource分配给您的持久化单元,而无需修改persistence.xml。你甚至可以离开< jta-data-source> < non-jta-data-source> 空和OpenEJB仍然会知道该怎么做。



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

 < persistence xmlns =http://java.sun.com/xml/ns/持久性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.SynchronizeMappingsvalue =buildSchema(ForeignKeys = true)/>
< / properties>
< / persistence-unit>
< / persistence>

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

  public void test()throws Exception {

userTransaction.begin();

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

列表<电影> list = movies.getMovies();
assertEquals(List.size(),3,list.size());

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

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

} finally {
userTransaction.commit();
}
}


What value should I place into <jta-data-source> of my 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:

[...]
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...

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

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)
[...]

解决方案

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 EntityManager in your test case very easily.

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.

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

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

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

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

@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 movieDatabase is 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.

But for the sake of completeness, here's how this particular application has defined the 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", "Reservoir 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();
    }
}

这篇关于持久性文件的jta-data-source的内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆