Fala Galera,
sou novato em java e estou tentando seguir um tutorial para criar um pool de conexões no tomcat e busca-lo com jndi só que estou me deparando com um NullPointerException pelo que diz meu log de erro no tomca e não consigo achar uma soluçao para o problema, o código e o stacktrace seguem abaixo:
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/TesteJndi" auth="Container">
<ResourceLink
name="jdbc/testeThiago"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/testesThiago"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="isquare"
maxActive="300"
maxIdle="30"
/>
</Context>
web.xml
<resource-ref>
<res-ref-name>jdbc/testeThiago</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
ServiceLocator
[code]
public class ServiceLocator {
public static Connection getConexao() throws Exception {
Connection conn = null;
try {
Context initCtx = new InitialContext();
DataSource ds =(DataSource)initCtx.lookup(“java:comp/env/jdbc/testeThiago”);
// ou
// Context envContext = (Context)initCtx.lookup(“java:comp/env”);
// DataSource ds = (DataSource) envContext.lookup(“jdbc/postgres”);
try {
conn = ds.getConnection();
} catch (SQLException e1) {
System.out.println(“ERRO: Nao conseguiu conectar”);
}
} catch (NamingException e) {
System.out.println(“Falha ao conectar”);
}
return conn;
}
}
meu arquivo para poder fazer o teste
public class ServiceLocator {
public static Connection getConexao() throws Exception {
Connection conn = null;
try {
Context initCtx = new InitialContext();
DataSource ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/testeThiago");
// ou
// Context envContext = (Context)initCtx.lookup("java:comp/env");
// DataSource ds = (DataSource) envContext.lookup("jdbc/postgres");
try {
conn = ds.getConnection();
} catch (SQLException e1) {
System.out.println("ERRO: Nao conseguiu conectar");
}
} catch (NamingException e) {
System.out.println("Falha ao conectar");
}
return conn;
}
}
e enfm o stacktrace do erro
10/05/2011 14:12:28 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at com.testejndi.teste.testeJndi.processRequest(testeJndi.java:53)
at com.testejndi.teste.testeJndi.doGet(testeJndi.java:77)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:706)
at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:677)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:54)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:662)
pelo que entendi foi lançada um NullPointerException quando é tentado executar a linha
conn = ds.getConnection();
mas não encontrei uma solução ainda, se alguem souber e puder me dar uma luz, agradeço desde já
Att,