Friday, February 04, 2005

jTDS - The fastest JDBC driver for MSSQL

jTDS is an open source 100% pure Java (type 4) JDBC driver for Microsoft SQL Server. It claims to be the fastest JDBC driver around and backs the claim with some benchmarks. All you have to do is drop a jar in the classpath, change your jdbc driver settings and url, and you're good to go, or so they say.

I dropped the jar file, modified my jdbc connection info as specified in their FAQs, but now my app won't start. I suspect it's my fault somehow, but it is a bit perplexing.

My app has some startup code that loops through all the resources in the environment context "java:comp/env". For some reason, my jTDS datasource is not found in the environment. If I replace the Driver and URL with the original Microsoft driver values, it does show up in the environment.

Here is my resource entry in Tomcat server.xml file:

<resource type="net.sourceforge.jtds.jdbcx.JtdsDataSource" auth="Container" name="mypool">
<resourceparams name="mypool">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>50</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>25</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>the_user</value>
</parameter>
<parameter>
<name>password</name>
<value>the_password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://server1:52500/dev_2.2;
user=the_user;password=the_password</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>SELECT 1</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>300</value>
</parameter>
</resourceparams>


And the code that fails is:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Object obj = envCtx.lookup("mypool");

After the last line of code above, obj is null. For some reason, the datasource is not getting into the environment context.

I posted the above problem on a jTDS forum (http://sourceforge.net/forum/forum.php?thread_id=1226037&forum_id=104389) and received a suggestion that fixed the problem.

I needed to change "net.sourceforge.jtds.jdbcx.JtdsDataSource" to "javax.sql.DataSource" in the <resource> element. I think I misunderstood the jTDS documentation. There was a table that had two columns, one column had "javax.sql.DataSource" and the other had "net.sourceforge.jtds.jdbcx.JtdsDataSource". I thought it was a simple "search and replace" job, but not so.

Anyway, it's up and running now!

No comments: