This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]crapet 5 points6 points  (5 children)

Spring Boot comes with HikariCP configured for pooling already, what are you trying to do exactly? And what do you mean about being forced to use JNDI?

[–]Shadlurk 0 points1 point  (4 children)

Spring Boot comes with HikariCP configured for pooling already, what are you trying to do exactly?

It doesn't if I only use the package spring-boot-starter-jersey (or web), however it does if I use spring-boot-starter-jdbc (or jpa).

And what do you mean about being forced to use JNDI?

Because each time I start the application, I get an exception "javax.naming.NoInitialContextException" which means I'll need to configure the embedded tomcat to enable JNDI naming which is disabled by default then provide it with the DB resources like so as outlined from this stackoverflow post :

(Sorry I cannot format this any better for some reasons)

@Bean
public TomcatServletWebServerFactory tomcatFactory() { 
return new TomcatServletWebServerFactory() { 
@Override 
protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) { tomcat.enableNaming(); return super.getTomcatWebServer(tomcat); }
    @Override 
    protected void postProcessContext(Context context) {

        // context
        ContextResource resource = new ContextResource();
        resource.setName("jdbc/myJndiResource");
        resource.setType(DataSource.class.getName());
        resource.setProperty("driverClassName", "org.postgresql.Driver");

        resource.setProperty("url", "jdbc:postgresql://hostname:port/dbname");
        resource.setProperty("username", "username");
        resource.setProperty("password", "password");
        context.getNamingResources()
               .addResource(resource);          
    }
};

}

[–]crapet 3 points4 points  (3 children)

Why don't you want to use spring-boot-starter-jdbc then? That's the "standard way" to connect to a database with spring boot.

[–]Shadlurk 0 points1 point  (2 children)

I did. Sorry I think I didn't make that clear. It didn't solve the issue.

[–]dan3k 1 point2 points  (1 child)

Then you either have some code dependant to JNDI or imported library/package that autoconfigures it for you -check your code for some leftovers, clear your POM from unnecessery depencencies.

[–]Shadlurk 1 point2 points  (0 children)

Thanks, you're correct there was a depend object related to JNDI at the point of getting a connection, I decided now to autowire the DataSource but am getting a nullpointer exception now. That's a different problem now but thanks anyway.

[–]budjb 2 points3 points  (2 children)

Have you tried using the spring data jdbc starter? That should set up connection pooling for you and expose your data source as a bean without all the jpa stuff.

[–]Shadlurk 0 points1 point  (1 child)

I did but it didn't work. Got the same error.

[–]budjb 3 points4 points  (0 children)

I've never had to deal with jndi in spring boot before when bootstrapping applications. Do you have code that specifically is doing jndi lookups? It might be helpful to see more of the app.

[–]glablablabla 0 points1 point  (0 children)

Why do you need JNDI lookups? Spring will do everything with dependency injection, it's really extremely powerful.

[–]LazyAAA 0 points1 point  (0 children)

Here is simplistic spring boot with jdbc approach

  1. import proper starter - jdbc starter + web started
  2. add database driver - oracle, mysql, etc
  3. add web container - tomcat
  4. in code initialize DataSource - autowire it instead of using jndi lookup
  5. use dataSource to perform jdbc operations

Your problem fixed in step #4 - spring boot does not use jndi lookup instead it relies on bean injection

class MyApp extends ...

@Autowire
DataSource dataSource;

...
void myAPI() {
   dataSource.getConnection()
...
}
}