//  ========================================================================
//  Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
//  ========================================================================
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================

[[jndi-datasource-examples]]
=== Datasource Examples

Here are examples of configuring a JNDI datasource for various databases.

____
[NOTE]
Read xref:configuring-datasources[] in xref:jndi-configuration[] for more information about configuring datasources.
____

All of these examples correspond to a `resource-ref` in `web.xml`.

[source, xml, subs="{sub-order}"]
----
  <resource-ref>
     <description>My DataSource Reference</description>
     <res-ref-name>jdbc/DSTest</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
  </resource-ref>
----

These examples assume that all of the datasources are declared at the JVM scope, but you can use other scopes if desired.
You can configure all JNDI resources in a `jetty.xml` file, a `WEB-INF/jetty-env.xml` file, or a context XML file.
See the section xref:jndi-where-to-declare[] for more information.

____
[IMPORTANT]
You must provide Jetty with the libraries necessary to instantiate the datasource you have configured by putting the corresponding Jar in `JETTY_HOME/lib/ext`.
____

[[pooling-datasources]]
==== Pooling DataSources

Pooling datasources enables connection pooling, which lets you reuse an existing connection instead of creating a new connection to the database.
This is highly efficient in terms of memory allocation and speed of the request to the database.
We highly recommend this option for production environments.

The following is a list of the pooled datasource examples we have worked with in the past:

* xref:hikaricp-datasource[]
* xref:bonecp-datasource[]
* xref:c3p0-datasource[]
* xref:dbcp-datasource[]
* xref:atomikos-datasource[]
* xref:mysql-pooled-datasource[]
* xref:postgreSQL-pooled-datasource[]
* xref:DB2-pooled-datasource[]

[[hikaricp-datasource]]
===== HikariCP

Connection pooling, available at http://search.maven.org/remotecontent?filepath=com/zaxxer/HikariCP/1.4.0/HikariCP-1.4.0.jar[HikariCP Download].
All configuration options for HikariCP are described here: https://github.com/brettwooldridge/HikariCP[HikariCP documentation].

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
       <New class="com.zaxxer.hikari.HikariDataSource">
         <Arg>
            <New class="com.zaxxer.hikari.HikariConfig">
               <Set name="minimumPoolSize">5</Set>
               <Set name="maximumPoolSize">20</Set>
               <Set name="dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</Set>
               <Set name="username">jdbc.user</Set>
               <Set name="password">jdbc.pass</Set>
               <Call name="addDataSourceProperty">
                  <Arg>url</Arg>
                  <Arg>jdbc.url</Arg>
               </Call>
            </New>
         </Arg>
      </New>
    </Arg>
  </New>
----

[[bonecp-datasource]]
===== BoneCP

Connection pooling, available at http://jolbox.com/index.html?page=http://jolbox.com/download.html[BoneCP Download].
All configuration options for BoneCP are described here: http://jolbox.com/bonecp/downloads/site/apidocs/com/jolbox/bonecp/BoneCPDataSource.html[BoneCP API].

[source, xml, subs="{sub-order}"]
----

  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
       <New class="com.jolbox.bonecp.BoneCPDataSource">
         <Set name="driverClass">com.mysql.jdbc.Driver</Set>
         <Set name="jdbcUrl">jdbc.url</Set>
         <Set name="username">jdbc.user</Set>
         <Set name="password">jdbc.pass</Set>
         <Set name="minConnectionsPerPartition">5</Set>
         <Set name="maxConnectionsPerPartition">50</Set>
         <Set name="acquireIncrement">5</Set>
         <Set name="idleConnectionTestPeriod">30</Set>
      </New>
    </Arg>
  </New>
----

[[c3p0-datasource]]
===== c3p0

Connection pooling, available at http://central.maven.org/maven2/c3p0/c3p0/0.9.1.2/c3p0-0.9.1.2.jar[c3p0 Jar].

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
      <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <Set name="driverClass">org.some.Driver</Set>
         <Set name="jdbcUrl">jdbc.url</Set>
         <Set name="user">jdbc.user</Set>
         <Set name="password">jdbc.pass</Set>
      </New>
     </Arg>
    </New>
----

[[dbcp-datasource]]
===== DBCP

Connection pooling, available at http://central.maven.org/maven2/commons-dbcp/commons-dbcp/1.2/commons-dbcp-1.2.jar[dbcp Jar].

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
         <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">org.some.Driver</Set>
            <Set name="url">jdbc.url</Set>
            <Set name="username">jdbc.user</Set>
            <Set name="password">jdbc.pass</Set>
         </New>
         </New>
     </Arg>
    </New>
----

[[atomikos-datasource]]
===== Atomikos 3.3.2+

Connection pooling + XA transactions.

[source, xml, subs="{sub-order}"]
----
   <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
      <Arg></Arg>
      <Arg>jdbc/DSTest</Arg>
      <Arg>
         <New class="com.atomikos.jdbc.AtomikosDataSourceBean">
            <Set name="minPoolSize">2</Set>
            <Set name="maxPoolSize">50</Set>
            <Set name="xaDataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</Set>
            <Set name="UniqueResourceName">DSTest</Set>
            <Get name="xaProperties">
               <Call name="setProperty">
                  <Arg>url</Arg>
                  <Arg>jdbc:mysql://localhost:3306/databasename</Arg>
               </Call>
               <Call name="setProperty">
                  <Arg>user</Arg>
                  <Arg>some_username</Arg>
               </Call>
               <Call name="setProperty">
                  <Arg>password</Arg>
                  <Arg>some_password</Arg>
               </Call>
            </Get>
         </New>
      </Arg>
    </New>
----

[[mysql-pooled-datasource]]
===== MySQL

Implements `javax.sql.DataSource` and `javax.sql.ConnectionPoolDataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
           <Set name="Url">jdbc:mysql://localhost:3306/databasename</Set>
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
        </New>
     </Arg>
    </New>
----

[[postgreSQL-pooled-datasource]]
===== PostgreSQL

Implements `javax.sql.ConnectionPoolDataSource`.

[source, xml, subs="{sub-order}"]
----

  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="org.postgresql.ds.PGConnectionPoolDataSource">
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="DatabaseName">dbname</Set>
           <Set name="ServerName">localhost</Set>
           <Set name="PortNumber">5432</Set>

        </New>
     </Arg>
  </New>


----

[[DB2-pooled-datasource]]
===== DB2

Implements `javax.sql.ConnectionPoolDataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="com.ibm.db2.jcc.DB2ConnectionPoolDataSource">
           <Set name="DatabaseName">dbname</Set>
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="ServerName">servername</Set>
           <Set name="PortNumber">50000</Set>
        </New>
     </Arg>
  </New>
----

[[non-pooling-datasources]]
==== Non-pooling DataSources

If you are deploying in a production environment, we highly recommend using a Pooling DataSource.
Since that is not always an option we have a handful of examples for non-pooling datasources listed here as well.

The following is a list of the non-pooled datasource examples:

* xref:sql-server-2000-datasource[]
* xref:oracle-9i10g-datasource[]
* xref:postgreSQL-datasource[]
* xref:sybase-datasource[]
* xref:DB2-datasource[]

[[sql-server-2000-datasource]]
===== SQL Server 2000

Implements `javax.sql.DataSource` and `javax.sql.ConnectionPoolDataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="DatabaseName">dbname</Set>
           <Set name="ServerName">localhost</Set>
           <Set name="PortNumber">1433</Set>
        </New>
     </Arg>
    </New>
----

[[oracle-9i10g-datasource]]
===== Oracle 9i/10g

Implements `javax.sql.DataSource` and `javax.sql.ConnectionPoolDataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/DSTest</Arg>
    <Arg>
      <New class="oracle.jdbc.pool.OracleDataSource">
        <Set name="DriverType">thin</Set>
        <Set name="URL">jdbc:oracle:thin:@fmsswdb1:10017:otcd</Set>
        <Set name="User">xxxx</Set>
        <Set name="Password">xxxx</Set>
        <Set name="connectionCachingEnabled">true</Set>
        <Set name="connectionCacheProperties">
          <New class="java.util.Properties">
            <Call name="setProperty">
              <Arg>MinLimit</Arg>
              <Arg>5</Arg>
            </Call>
            <!-- put the other properties in here too -->
          </New>
        </Set>
      </New>
    </Arg>
  </New>
----

For more information, refer to: http://docs.oracle.com/cd/B14117_01/java.101/b10979/conncache.htm[Oracle Database JDBC documentation].

[[postgreSQL-datasource]]
===== PostgreSQL

Implements `javax.sql.DataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="org.postgresql.ds.PGSimpleDataSource">
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="DatabaseName">dbname</Set>
           <Set name="ServerName">localhost</Set>
           <Set name="PortNumber">5432</Set>
        </New>
     </Arg>
  </New>
----

[[sybase-datasource]]
===== Sybase

Implements `javax.sql.DataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="com.sybase.jdbc2.jdbc.SybDataSource">
           <Set name="DatabaseName">dbname</Set>
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="ServerName">servername</Set>
           <Set name="PortNumber">5000</Set>
        </New>
     </Arg>
  </New>
----

[[DB2-datasource]]
===== DB2

Implements `javax.sql.DataSource`.

[source, xml, subs="{sub-order}"]
----
  <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg></Arg>
     <Arg>jdbc/DSTest</Arg>
     <Arg>
        <New class="com.ibm.db2.jcc.DB2SimpleDataSource">
           <Set name="DatabaseName">dbname</Set>
           <Set name="User">user</Set>
           <Set name="Password">pass</Set>
           <Set name="ServerName">servername</Set>
           <Set name="PortNumber">50000</Set>
        </New>
     </Arg>
  </New>
----
