Skip to content

Instantly share code, notes, and snippets.

@webdevwilson
Created February 29, 2012 16:45
Show Gist options
  • Save webdevwilson/1942363 to your computer and use it in GitHub Desktop.
Save webdevwilson/1942363 to your computer and use it in GitHub Desktop.
JndiDataSource
import com.google.inject.AbstractModule;
import javax.naming.InitialContext;
public class DataSource
{
public static AbstractModule fromJndi(final String jndiName)
{
return new AbstractModule()
{
@Override
protected void configure()
{
try
{
final InitialContext ic = new InitialContext();
final javax.sql.DataSource dataSource = (javax.sql.DataSource) ic.lookup(jndiName);
bind(javax.sql.DataSource.class).toInstance(dataSource);
}
catch (final Exception e)
{
throw new RuntimeException("Error retrieving datasource '" + jndiName + "'", e);
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment