简体   繁体   English

使用Maven嵌入式Glassfish配置外部数据库

[英]configure external database with maven embedded-glassfish

I need to create educational demo of java web-application using EJB. 我需要使用EJB创建Java Web应用程序的教学演示。

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). 我想使用maven Embedded-glassfish插件来简化将要运行此演示的人员的工作(因此,他们无需手动设置和配置glassfish服务器)。

However, I could not understand how to force embedded-glassfish to use other database rather than temporary apache derby. 但是,我不明白如何强制嵌入式Glassfish使用其他数据库而不是临时的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). 我使用Java Persistence API-我希望用户使用由我的应用程序启动的永久数据库,例如H2(它可以启动)。

I've tried straightforward idea - to configure what I need by persistence.xml 我尝试了简单明了的想法-通过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. 它不会被完全忽略,它会被加载-但是我的JPA仍然可以使用默认数据库。 What can I do? 我能做什么? Can I reconfigure jdbc/__default datasource somehow, or make my persistence file work? 我可以以某种方式重新配置jdbc / __ default数据源,还是可以使我的持久性文件正常工作? Thanks in advance! 提前致谢!

In a JavaEE container context it's unusal to configure the JDBC connection directly in the persistence unit. 在JavaEE容器上下文中,直接在持久性单元中配置JDBC连接是不寻常的。 Instead, the JDBC connection is being configured separately as data source, which is being referenced in the persistence unit via the javax.persistence.jtaDataSource property (this gives the container a chance to manage connection pooling etc). 取而代之的是,JDBC连接被单独配置为数据源,通过javax.persistence.jtaDataSource属性在持久性单元中进行引用(这使容器有机会管理连接池等)。

Because the jtaDataSource is not being set, the container "falls back" to the default data source under "jdbc/__default". 因为未设置jtaDataSource,所以容器“回退”到“ jdbc / __ default”下的默认数据源。

My suggestion is that you configure your own datasource that can be referenced in the persistence.xml. 我的建议是您配置自己的数据源,该数据源可在persistence.xml中引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM