deamon
deamon

Reputation: 92437

How to configure DB connection in a Servlet based JPA application

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)?

Upvotes: 2

Views: 3228

Answers (4)

Romain Hippeau
Romain Hippeau

Reputation: 24375

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

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

<?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>

Upvotes: 2

Pascal Thivent
Pascal Thivent

Reputation: 570365

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:

<?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>

Upvotes: 1

ZZ Coder
ZZ Coder

Reputation: 75456

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

Upvotes: 0

duffymo
duffymo

Reputation: 308763

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.

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

Upvotes: 0

Related Questions