Jetty DataSource Examples

DataSource Examples

Here are some examples of setting up a DataSource in JNDI for various databases.

These examples all correspond to a <resource-ref> in web.xml like:

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

For convenience, we will assume that all of the datasources are declared at the jvm scope, but you can of course use other scopes, as discussed here.

Don't forget that all JNDI resources can be configured in a jetty.xml file or in a WEB-INF/jetty-env.xml file, or a context xml file. More information on that can be found on the page on JNDI.

Pooling DataSources

Enables connection pooling.
Connection pooling is basically re-using existing connections instead of creating a new connection to the database.
This would be highly efficient in terms of memory allocation and speed of the request to the database.
In production, this is highly recommended.

c3p0 (connection pooling)

available at http://repo1.maven.org/maven2/c3p0/c3p0/0.9.1.2/c3p0-0.9.1.2.jar
<New id="DSTest" class="org.mortbay.jetty.plus.naming.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 (connection pooling)

available at http://repo1.maven.org/maven2/commons-dbcp/commons-dbcp/1.2/commons-dbcp-1.2.jar
<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>
    </Arg>
   </New>

Atomikos 3.3.2+ (connection pooling + XA transactions)

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>

Non-pooling DataSources

If you're deploying in production environment, use the Pooling DataSources instead.

MySQL

implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>

SQL Server 2000

implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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 9i/10g

implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg></Arg>
    <Arg>jdbc/DSTest</Arg>
    <Arg>
     <New class="oracle.jdbc.pool.OracleConnectionPoolDataSource">
                 <Set name="URL">jdbc:oracle:thin:@localhost:1521:orcl</Set>
                 <Set name="User">user</Set>
                 <Set name="Password">pass</Set>

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

PostgreSQL

implements javax.sql.DataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>

implements javax.sql.ConnectionPoolDataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>

Sybase

implements javax.sql.DataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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

implements javax.sql.DataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>

implements javax.sql.ConnectionPoolDataSource

<New id="DSTest" class="org.mortbay.jetty.plus.naming.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值