Wednesday, 13 June 2012

SQL Server & Tomcat

JTDS Library

It is necessary sometimes to connect Tomcat to MS SQL server using SSO from the user currently logged into the windows machine they are on.  This is possible with the jtds (http://jtds.sourceforge.net/) library.  This brilliant library requires a small dll file (ntlmauth.dll) to be put on the system path (eg jre/bin) and the jtds jar file to be put into the tomcat lib directory.  The dll interacts with Windows to get the user credentials and the jtds jar does the normal jdbc connection bits.

Tomcat context.xml

The context file now needs to have the resource added.  The username and password elements are not required if you want the SSO.


    <Resource
       name="jdbc/myDS"
       auth="Container"
       type="javax.sql.DataSource"
       maxActive="100"
       maxIdle="30"
       maxWait="10000"
       driverClassName="net.sourceforge.jtds.jdbcx.JtdsDataSource"
       url="jdbc:jtds:sqlserver://<servername>:<port>/<databasename>;instance=<instance>;
          domain=<userdomain>"/>

It is necessary to specify the instance because the SQL Server may specify a

    Server name: servername\instance

but this isn't a valid network servername.  Therefore this has to be split into two parts.  The actual server name and the instance.  The port is usually 1433 for SQL Server.



No comments:

Post a comment