JDBC, POOL, JPA, and Tomcat 7 Configuration

if you want to configure JDBC POOL  JPA within Tomcat app server, I have an example for this case.

1. First you must edit context.xml, add the “Resource” tag.

file: context.xml

<br />
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />
&lt;Context antiJARLocking=&quot;true&quot; path=&quot;/orchestra&quot;&gt;<br />
&lt;Resource name=&quot;jdbc/dbsource&quot; auth=&quot;Container&quot; type=&quot;javax.sql.DataSource&quot;<br />
factory=&quot;org.apache.tomcat.jdbc.pool.DataSourceFactory&quot; driverClassName=&quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot; username=&quot;sa&quot; password=&quot;sa&quot; url=&quot;jdbc:sqlserver://localhost:1433;databaseName=mydb;user=sa;password=sa;&quot; testOnBorrow=&quot;true&quot; validationQuery=&quot;SELECT 1&quot; /&gt;<br />
&lt;/Context&gt;<br />

2. Link the “Resource” tag to your application, edit web.xml and add this line within the <web-app> tag

file: web.xml

<br />
&lt;resource-ref&gt;<br />
        &lt;description&gt;DB Connection&lt;/description&gt;<br />
        &lt;res-ref-name&gt;jdbc/orchestra&lt;/res-ref-name&gt;<br />
        &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;<br />
        &lt;res-auth&gt;Container&lt;/res-auth&gt;<br />
    &lt;/resource-ref&gt;<br />

3. Create persistance.xml, copy this following code

<br />
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />
&lt;persistence version=&quot;2.0&quot; xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot;&gt;<br />
  &lt;persistence-unit name=&quot;orchestraPU&quot; transaction-type=&quot;JTA&quot;&gt;<br />
    &lt;jta-data-source&gt;java:/comp/env/jdbc/orchestra&lt;/jta-data-source&gt;<br />
    &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;<br />
    &lt;properties/&gt;<br />
  &lt;/persistence-unit&gt;<br />
&lt;/persistence&gt;<br />

Komentar komentar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s