Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestingJndiDatasource {
- private static final String driver = "org.postgresql.Driver";
- private static final String url = "jdbc:postgresql://localhost/dotcms";
- private static final String username = "postgres";
- private static final String password = "postgres";
- private static final int maxTotal = 60;
- private static Hashtable<String, DataSource> dataSources = new Hashtable<>();
- public static void init() throws Exception {
- NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {
- @Override
- public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment)
- throws NamingException {
- return new InitialContextFactory() {
- @Override
- public Context getInitialContext(Hashtable<?, ?> environment)
- throws NamingException {
- return new InitialContext() {
- @Override
- public Object lookup(String name) throws NamingException {
- if (dataSources.isEmpty()) { // init datasources
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName(driver);
- dataSource.setUrl(url);
- dataSource.setUsername(username);
- dataSource.setPassword(password);
- dataSource.setRemoveAbandoned(true);
- dataSource.setLogAbandoned(true);
- dataSource.setMaxIdle(10);
- dataSource.setMaxActive(60);
- dataSources.put("jdbc/dotCMSPool", dataSource);
- }
- if (dataSources.containsKey(name)) {
- return dataSources.get(name);
- }
- throw new NamingException("Unable to find datasource: " + name);
- }
- };
- }
- };
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement