Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. /**
  2. * Returns a database connection for the specified datasource
  3. * @param db - ex. "jdbc/DatasourceXyz"
  4. * @return db Connection
  5. * @throws Exception
  6. */
  7. public static Connection getDbConnection(String db) throws Exception {
  8. java.sql.Connection conn = null;
  9.  
  10. try {
  11. LOG.debug("getting db connection for " + db + " ...");
  12.  
  13. javax.naming.Context initCtx = new javax.naming.InitialContext();
  14. javax.naming.Context envCtx = (javax.naming.Context) initCtx.lookup("java:comp/env");
  15. javax.sql.DataSource ds = (javax.sql.DataSource) envCtx.lookup(db);
  16. conn = ds.getConnection();
  17.  
  18. initCtx.close();
  19. LOG.debug("got db connection for " + db);
  20. LOG.debug("url = " + conn.getMetaData().getURL());
  21.  
  22. } catch (Exception e) {
  23. String message = String.format("Exception thrown while creating a JDBC connection for %s: %s", db,
  24. e.getMessage());
  25. throw new Exception(message);
  26. }
  27.  
  28. return conn;
  29. }
  30.  
  31. <Context>
  32. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  33. <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
  34. <!-- environment-specific jdbc data sources: -->
  35. <Resource name="jdbc/DatasourceXyz" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@hostname:1521:database" username="someUser" password="***" maxTotal="20" maxIdle="10" maxWaitMillis="60000"/>
  36. <!-- set other jdbc data sources below: -->
  37.  
  38. public static Connection getDbConnection(String db) throws Exception {
  39. java.sql.Connection conn = null;
  40.  
  41. try {
  42. LOG.debug("getting db connection for " + db + " ...");
  43.  
  44. // Get the SampleUtilService from the Bridge
  45. SampleUtilService sampleUtilService = SpringContextBridge.services().getSampleUtilService();
  46.  
  47. // Get DataSource from the JndiObjectFactoryBean
  48. javax.sql.DataSource ds = sampleUtilService.getDataSourcefromFactoryBean(db);
  49.  
  50. conn = ds.getConnection();
  51.  
  52. LOG.debug("got db connection for " + db);
  53. LOG.debug("url = " + conn.getMetaData().getURL());
  54.  
  55. } catch (Exception e) {
  56. String message = String.format("Exception thrown while creating a JDBC connection for %s: %s", db,
  57. e.getMessage());
  58. throw new Exception(message);
  59. }
  60.  
  61. return conn;
  62. }
  63.  
  64. @Bean(destroyMethod="")
  65. public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
  66. JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
  67. bean.setJndiName("java:comp/env/jdbc/myDataSource");
  68. bean.setProxyInterface(DataSource.class);
  69. bean.setLookupOnStartup(false);
  70. bean.afterPropertiesSet();
  71. return (DataSource)bean.getObject();
  72. }
  73.  
  74. DataSource dataSource =
  75. (DataSource) new InitialContext().lookup("java:comp/env/jdbc/myDataSource");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement