Advertisement
Guest User

Untitled

a guest
Sep 16th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public class TestingJndiDatasource {
  2.  
  3. private static final String driver = "org.postgresql.Driver";
  4. private static final String url = "jdbc:postgresql://localhost/dotcms";
  5. private static final String username = "postgres";
  6. private static final String password = "postgres";
  7. private static final int maxTotal = 60;
  8. private static Hashtable<String, DataSource> dataSources = new Hashtable<>();
  9.  
  10.  
  11. public static void init() throws Exception {
  12.  
  13. NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {
  14.  
  15.  
  16. @Override
  17. public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment)
  18. throws NamingException {
  19. return new InitialContextFactory() {
  20.  
  21. @Override
  22. public Context getInitialContext(Hashtable<?, ?> environment)
  23. throws NamingException {
  24. return new InitialContext() {
  25.  
  26. @Override
  27. public Object lookup(String name) throws NamingException {
  28.  
  29. if (dataSources.isEmpty()) { // init datasources
  30.  
  31. BasicDataSource dataSource = new BasicDataSource();
  32. dataSource.setDriverClassName(driver);
  33. dataSource.setUrl(url);
  34. dataSource.setUsername(username);
  35. dataSource.setPassword(password);
  36. dataSource.setRemoveAbandoned(true);
  37. dataSource.setLogAbandoned(true);
  38. dataSource.setMaxIdle(10);
  39. dataSource.setMaxActive(60);
  40. dataSources.put("jdbc/dotCMSPool", dataSource);
  41.  
  42. }
  43.  
  44. if (dataSources.containsKey(name)) {
  45. return dataSources.get(name);
  46. }
  47.  
  48. throw new NamingException("Unable to find datasource: " + name);
  49. }
  50. };
  51. }
  52.  
  53. };
  54. }
  55.  
  56. });
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement