1.下载jetty服务器(8.1.0.RC2),解压到任意目录下
http://dist.codehaus.org/jetty/jetty-hightide-8.1.0/jetty-hightide-8.1.0.RC2.zip
2.新建jetty-dataSource.xml文件,放在${JETTY_HOME}\contexts目录下
这个例子新建了两个mysql数据源,
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <!-- ================================================================== Configure and deploy the test web application in $(jetty.home)/webapps/test Note. If this file did not exist or used a context path other that /test then the default configuration of jetty.xml would discover the test webapplication with a WebAppDeployer. By specifying a context in this directory, additional configuration may be specified and hot deployments detected. ===================================================================== --> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/datasource</Set> <Set name="resourceBase">./</Set> <New id="showcase" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <Arg>jdbc/showcase</Arg> <Arg> <New class="com.mchange.v2.c3p0.ComboPooledDataSource"> <Set name="driverClass">com.mysql.jdbc.Driver</Set> <Set name="jdbcUrl">jdbc:mysql://localhost:3306/showcase?useUnicode=true&characterEncoding=UTF8</Set> <Set name="user">root</Set> <Set name="password">111111</Set> </New> </Arg> </New> <New id="quartz" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <Arg>jdbc/quartz</Arg> <Arg> <New class="com.mchange.v2.c3p0.ComboPooledDataSource"> <Set name="driverClass">com.mysql.jdbc.Driver</Set> <Set name="jdbcUrl">jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF8</Set> <Set name="user">root</Set> <Set name="password">111111</Set> </New> </Arg> </New> </Configure>
3.将数据源用到的jar包放到${JETTY_HOME}\lib\ext目录下
因为以上用到了c3p0数据库连接池和mysql数据库,因此需要将mysql-connector-java-5.1.17.jar和c3p0-0.9.1.2.jar放到jetty容器的lib中
4.重启jetty容器
命令行到${JETTY_HOME}目录下,运行 java -jar start.jar启动容器
5.附:常见的jndi数据库链接池配置
参考:http://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
Pooling DataSources
Enables connection pooling. Connection pooling 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.
BoneCP
Connection pooling, available at http://jolbox.com/index.html?page=http://jolbox.com/download.html All configuration options for BoneCP are described here: http://jolbox.com/bonecp/downloads/site/apidocs/com/jolbox/bonecp/BoneCPDataSource.html
<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
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.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
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.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> <Set name="validationQuery">SELECT 1</Set> </New> </Arg> </New>
Note: validationQuery is optional, and works together with the testOnBorrow parameter (which defaults to true but has no effect unless validationQuery is specified) to help ensure that old connections are valid when used. See http://commons.apache.org/dbcp/configuration.html
Atomikos 3.3.2+
Connection pooling + XA transactions.
<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>
Non-pooling DataSources
If you are deploying in a production environment, use the Pooling DataSources instead.
MySQL
Implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource
<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>
SQL Server 2000
Implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource
<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 9i/10g
Implements javax.sql.DataSource, javax.sql.ConnectionPoolDataSource
<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 please see http://download.oracle.com/docs/cd/B14117_01/java.101/b10979/conncache.htm#CDEBCBJC.
PostgreSQL
Implements javax.sql.DataSource
<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>
Implements javax.sql.ConnectionPoolDataSource
<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>
Sybase
Implements javax.sql.DataSource
<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
Implements javax.sql.DataSource
<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>
Implements javax.sql.ConnectionPoolDataSource
<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>