0

I need to create educational demo of java web-application using EJB.

I want to use maven embedded-glassfish plugin to simplify matter for people who will run this demo (so that they need not manually set up and configure glassfish server).

However, I could not understand how to force embedded-glassfish to use other database rather than temporary apache derby. I use Java Persistence API - and I want users to use permanent database, for example H2, started by my application (it starts all right).

I've tried straightforward idea - to configure what I need by persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="demoData" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://127.0.0.1:12345/demodb"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
        </properties>
    </persistence-unit>

</persistence>

It is not ignored completely, it is loaded - but my JPA works with default database anyway. What can I do? Can I reconfigure jdbc/__default datasource somehow, or make my persistence file work? Thanks in advance!

4

1 回答 1

0

在 JavaEE 容器上下文中,直接在持久性单元中配置 JDBC 连接是不常见的。相反,JDBC 连接被单独配置为数据源,通过 javax.persistence.jtaDataSource 属性在持久性单元中引用(这使容器有机会管理连接池等)。

因为没有设置 jtaDataSource,所以容器“回退”到“jdbc/__default”下的默认数据源。

我的建议是您配置自己的数据源,可以在persistence.xml 中引用。

于 2013-01-02T14:43:16.603 回答