如何在基于Servlet的JPA应用程序中配置DB连接

时间:2022-01-03 13:18:11

By default DB connections of JPA applications are configured in the META-INF/persistence.xml, when the application is not deployed to a full Java EE application server. In my opinion it is not very elegant to place such environment specific configuration into a file that is inside a .war file. How could a DB connection of a Servlet based JPA application be configured more flexible (=outside of the .war file)?

默认情况下,JPA应用程序的DB连接在META-INF/持久性中配置。当应用程序没有部署到完整的Java EE应用服务器时。在我看来,将这种特定于环境的配置放在.war文件中的文件中并不是很合适。基于Servlet的JPA应用程序的DB连接如何配置得更灵活(=在.war文件之外)?

4 个解决方案

#1


2  

You do not place environment specific stuff in the persistence.xml. There are two levels of indirection 1) In your persistence.xml you will have something like ...

在persistence.xml中不放置特定于环境的内容。在你的坚持中有两个层次的间接。您将拥有类似于…

<persistence 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"
             version="2.0">
   <persistence-unit name="sample">
      <jta-data-source>java:comp/env/jdbc/DefaultDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="..."/>
         <property name="hibernate.hbm2ddl.auto" value="..."/>
      </properties>
   </persistence-unit>
</persistence>

Let's assume you are using Tomcat, then you would define the DataSource in Tomcat and that is where your actual DataSource Configuration would take place.

让我们假设您正在使用Tomcat,然后您将在Tomcat中定义数据源,这就是实际的数据源配置发生的地方。

2) In your CONTEXT.XML put the following code...

2)在你的上下文。XML放置了以下代码…

<?xml version="1.0" encoding="UTF-8"?>

<Context>

  <Resource name="jdbc/DefaultDS" auth="Container"
            type="javax.sql.DataSource" username="wally" password="wally"
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
            url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"
            maxActive="8" 
            />

</Context>

#2


1  

In a Servlet container, use also a datasource and a non-JTA (RESOURCE_LOCAL) persistence.xml. Declare your datasource in the non-jta-data-source element:

在一个Servlet容器中,还可以使用一个数据源和一个非jta (RESOURCE_LOCAL) persistence.xml。在非jta数据源元素中声明数据源:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
 <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
   <non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>
   <properties/>
 </persistence-unit>

#3


0  

Put a JNDI lookup name in the persistence.xml instead of the raw connection parameters. That externalizes the actual connection information so you can set it in the app server, where it belongs.

在持久性中放入JNDI查找名。xml而不是原始连接参数。这外部化了实际的连接信息,因此您可以在应用服务器中设置它,在它所属的位置。

Another way to do it might be to put connection parameters in a database.

另一种方法是将连接参数放入数据库中。

#4


0  

Not sure if it works for your provider, this is what I use,

不确定它是否适用于你的提供者,这是我使用的,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#JNDI_Datasource_Setup

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial JNDI_Datasource_Setup

#1


2  

You do not place environment specific stuff in the persistence.xml. There are two levels of indirection 1) In your persistence.xml you will have something like ...

在persistence.xml中不放置特定于环境的内容。在你的坚持中有两个层次的间接。您将拥有类似于…

<persistence 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"
             version="2.0">
   <persistence-unit name="sample">
      <jta-data-source>java:comp/env/jdbc/DefaultDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="..."/>
         <property name="hibernate.hbm2ddl.auto" value="..."/>
      </properties>
   </persistence-unit>
</persistence>

Let's assume you are using Tomcat, then you would define the DataSource in Tomcat and that is where your actual DataSource Configuration would take place.

让我们假设您正在使用Tomcat,然后您将在Tomcat中定义数据源,这就是实际的数据源配置发生的地方。

2) In your CONTEXT.XML put the following code...

2)在你的上下文。XML放置了以下代码…

<?xml version="1.0" encoding="UTF-8"?>

<Context>

  <Resource name="jdbc/DefaultDS" auth="Container"
            type="javax.sql.DataSource" username="wally" password="wally"
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
            url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"
            maxActive="8" 
            />

</Context>

#2


1  

In a Servlet container, use also a datasource and a non-JTA (RESOURCE_LOCAL) persistence.xml. Declare your datasource in the non-jta-data-source element:

在一个Servlet容器中,还可以使用一个数据源和一个非jta (RESOURCE_LOCAL) persistence.xml。在非jta数据源元素中声明数据源:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
 <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
   <non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>
   <properties/>
 </persistence-unit>

#3


0  

Put a JNDI lookup name in the persistence.xml instead of the raw connection parameters. That externalizes the actual connection information so you can set it in the app server, where it belongs.

在持久性中放入JNDI查找名。xml而不是原始连接参数。这外部化了实际的连接信息,因此您可以在应用服务器中设置它,在它所属的位置。

Another way to do it might be to put connection parameters in a database.

另一种方法是将连接参数放入数据库中。

#4


0  

Not sure if it works for your provider, this is what I use,

不确定它是否适用于你的提供者,这是我使用的,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#JNDI_Datasource_Setup

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial JNDI_Datasource_Setup