Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] Transactions necessary for db pooling with JNDI??

JAN THANK YOU SO MUCH!
(SORRY FOR SHOUTING)

Works perfectly. Thanks for responding to my (as it turned out)
non-jetty-dev-question.
Had been using Tomcat before and configured all in
META-INF/context.xml which is sort of a Tomcat-only shortcut to doing
this.

I am now fully converted to Jetty.

/Will

Here is a little writeup for those converting from Tomcat to Jetty
(recommended).
This is one way to set up db-pooling with MySQL:

WEB-INF/jetty-env.xml:
=====
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd";>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
	<New id="DataSource" class="org.eclipse.jetty.plus.jndi.Resource">
		<Arg>jdbc/JNDI_NAME</Arg>
		<Arg>
			<New class="org.apache.commons.dbcp.BasicDataSource">
				<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
				<Set name="url">jdbc:mysql://localhost/DB_NAME</Set>
				<Set name="username">DB_USER</Set>
				<Set name="password">DB_PASS</Set>
			</New>
		</Arg>
	</New>
</Configure>
=====

WEB-INF/web.xml (snippet from file); this is what I forgot:
=====
<resource-ref>
	<res-ref-name>jdbc/JNDI_NAME</res-ref-name>
	<res-type>org.apache.commons.dbcp.BasicDataSource</res-type>
	<res-auth>Container</res-auth>
</resource-ref>
=====

Put appropriate versions of these jars in your lib/ext:
=====
commons-dbcp-1.2.jar
commons-pool-1.4.jar
mysql-connector-java-5.1.6.jar
=====

In code get a datasource like so:
DataSource ds = (DataSource) (new
InitialContext()).lookup("java:comp/env/jdbc/JNDI_NAME");


If you use IBATIS (recommended) you never mess with DataSources directly:

To execute SQL you instead simply issue
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/com/company/project/sqlmap/config.xml"));

To make this work add the ibatis2-common-2.1.6.589.jar to lib/ext and
include an sqlmap-config file like this:
=====
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
	PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
	"http://ibatis.apache.org/dtd/sql-map-config-2.dtd";
>
<sqlMapConfig>
	<transactionManager type="JDBC" >
		<dataSource type="JNDI">
			<property
				name="DataSource"
				value="java:comp/env/jdbc/JNDI_NAME"
			/>
		</dataSource>
	</transactionManager>
	<sqlMap resource="com/company/project/sqlmap/Data.xml" />
</sqlMapConfig>
=====


Back to the top