Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am working on a Java project that runs on Linux/WebLogic that needs to integrate with an existing SQL Server instance that only allows authentication through a windows domain account. I've seen .Net applications use impersonation to connect and run the query as the domain account with the permissions.

Is there an equivalent way to do this with Java?

share|improve this question

marked as duplicate by Damian Leszczyński - Vash, RobV, madth3, explunit, GSee Jul 10 '13 at 1:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Did you mean a LDAP server? –  Paul Vargas Jul 9 '13 at 14:58
    
No, it's a Java application integrating with a Windows ecosystem. –  rynmrtn Jul 10 '13 at 13:10

1 Answer 1

up vote 0 down vote accepted

While this question is closely related to the potential duplicate, I'm going to post my answer in the hope that it is helpful to someone in the future.

What we did was define a non-jndi data source:

<bean id="nonJndiDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driver" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="initialSize" value="${initial_size}"/>
    <property name="maxActive" value="${max_active}"/>
    <property name="maxWait" value="${max_wait}"/>
    <property name="minIdle" value="${min_idle}"/>
    <property name="maxIdle" value="${max_idle}"/>
</bean>

With the following properties file (that sets the values in the bean):

# Hibernate specific property
dialect=org.hibernate.dialect.SQLServer2008Dialect

# This is the key line
url=jdbc:jtds:sqlserver://127.0.0.1;databaseName=yourDatabase;useNTLMv2=true;domain=nameOfDomain

user=windows_account
password=password
initial_size=5
max_active=30
max_wait=600000
min_idle=0
max_idle=10

This can then be hooked into JDBC, Hibernate, JDBI, or some other JDBC based framework.

One gotcha at the time of this post is the version of jTDS. Version 1.3 requires Java 7 - we ended up pulling the jar for Version 1.2.x.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.