连接到远程数据库服务器

时间:2015-05-15 16:37:35

标签: mysql java-ee jpa jdbc jta

我正在容器管理环境(Java EE)中开发应用程序。因此,我不必担心处理和关闭交易。我只是注入EntityManager:

@PersistenceContext(unitName = "WebApplication")
private EntityManager em;

我相应的persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="WebApplication" transaction-type="JTA">
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://link-to-my-database.us-east-1.rds.amazonaws.com:3306/TestDB"></property>
        <property name="javax.persistence.jdbc.user" value="admin"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
</persistence-unit>

然而,这不起作用。根据{{​​3}},javax.persistence.jdbc.driver出现在Java SE环境中使用。&#34; (第8.2.1.9节;第317页)。甚至只有jta-datasourcenon-jta-datasource&#34;在本地环境中命名数据源&#34; (第8.2.1.5节;第313页)。

然后我如何连接到远程数据库服务器?显然,我可以显式创建一个EntityManager但是我将负责处理事务,那么存在的意义是什么在容器管理环境中?如果可以使用JPA和容器管理环境连接到远程数据库服务器,您能提供一个如何操作的示例吗?

0 个答案:

没有答案
相关问题