0

I have defined the resource as following in conf in /var/lib/tomcat9/conf/server.xml

<GlobalNamingResources>
   <Resource name="jdbc/myservice" auth="Container" type="javax.sql.DataSource"
          // credentials for db - password and username as well
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://localhost:5432/myservice"
          maxTotal="25"
          maxIdle="10"
          validationQuery="select 1" /> 
</GlobalNamingResources>

and ResourceLink my /var/lib/tomcat9/conf/context.xml:

<Context>
   <ResourceLink 
    name="jdbc/myservice" 
    global="jdbc/myservice" 
    type="javax.sql.DataSource"/>
</Context>

when I am trying to test the Context:

@SpringBootApplication
public class MailServiceApp {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(MailServiceApp.class, args);
        InitialContext cxt = new InitialContext();

        DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/myservice" );
    }
}

I get the following exception:

Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial

I've also tried, still is not working

 DataSource ds = (DataSource) cxt.lookup( "jdbc/myservice" );
obolen_an
  • 41
  • 6
  • Does this answer your question? [How to create JNDI context in Spring Boot with Embedded Tomcat Container](https://stackoverflow.com/questions/24941829/how-to-create-jndi-context-in-spring-boot-with-embedded-tomcat-container) – Piotr P. Karwasz Sep 15 '21 at 11:57
  • @PiotrP.Karwasz thank you for the advice, but the configuration I've provided turned out to be correct. I solved the issue with editing tomcat run configuration in my IntelliJ IDEA – obolen_an Oct 23 '21 at 14:22

0 Answers0