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" );