Guest User

Untitled

a guest
Jan 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. DataSource dataSource = null;
  2. try
  3. {
  4. InitialContext ctx = new InitialContext();
  5. // comp/env is for component-environment bindings, such as web application bindings
  6. // this allows us to change the name later in the binding (in the ibm-web-bnd.xml file), instead of here in the code.
  7.  
  8. dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/refDB");
  9. }
  10. catch (NamingException e)
  11. {
  12. e.printStackTrace();
  13. }
  14.  
  15. return dataSource.getConnection();
  16.  
  17. System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
  18. System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
  19.  
  20. InitialContext ctx = new InitialContext();
  21.  
  22. //"java:comp/env/jdbc/CacheDb" is the context that needs creating here.
  23. ctx.createSubcontext("java:");
  24. ctx.createSubcontext("java:comp");
  25. ctx.createSubcontext("java:comp/env");
  26. ctx.createSubcontext("java:comp/env/jdbc");
  27.  
  28. JdbcDataSource dataSource = new JdbcDataSource();
  29. dataSource.setURL("jdbc:h2:mem:cachedb;DB_CLOSE_DELAY=-1"); // don't delete the database when the last connection closes
  30.  
  31. ctx.bind("java:comp/env/jdbc/CacheDb", dataSource);
  32.  
  33. System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
  34. System.setProperty(Context.URL_PKG_PREFIXES, "com.ibm.websphere.naming");
Add Comment
Please, Sign In to add comment