Last active
August 29, 2015 14:27
-
-
Save JeremyMcCormick/ba2258bca38317a69695 to your computer and use it in GitHub Desktop.
jndi example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SomeConstructor(){ | |
this.dataSource = initDatasource("jdbc/mysqldb01"); | |
} | |
private static DataSource initDatasource(String jndi){ | |
try { | |
javax.naming.Context ctx = new InitialContext(); | |
// relative to standard JNDI root for J2EE app | |
javax.naming.Context envCtx = (javax.naming.Context) ctx.lookup( "java:comp/env" ); | |
return (DataSource) envCtx.lookup( jndi ); | |
} catch(NamingException ex) { | |
throw new IllegalStateException(ex); | |
} | |
} | |
public doRequest(...){ | |
try(Connection conn = this.dataSource.getConnection()){ | |
doSomething(conn, param1, param2...); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment