Trouble Using StoredProcedure; DataSource gets Nulled
Doing my first stored procedure in Spring 3, and hitting a snag I haven't been able to figure out. According to the docs and online examples, it's right, but the DataSource is getting nulled when the service method is called that calls the procedure.
Log output confirms the datasource is being set from the bean on startup, and confirms that the datasource (in myDaoImpl) is null after the service method call.
I must be doing something stupid. Anyone have any thoughts?
In jdbc-context.xml (using a jdbc.properties file which works using a SimpleJdbcTemplate example):
Code:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="myDaoImpl" class="com.mydomain.myapp.dao.MyDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
In myDaoImpl:
Code:
public class myDaoImpl implements myDao {
...
private DataSource dataSource;
...
@Autowired
public void setDataSource(DataSource dataSource) {
LOGGER.info("setDataSource() called with " + dataSource.toString()); <- log confirms works
this.dataSource = dataSource;
}
public String callProc(long id) {
if (this.dataSource == null) {
LOGGER.info("dataSource is NULL!"); <- get this in the log after the setDataSource above
}
else {
LOGGER.info("dataSource is " + this.dataSource.toString());
}
StoredProcedureMyDao proc = new StoredProcedureMyDao(this.dataSource);
String result = proc.execute(id);
LOGGER.info(result);
return result;
}
In the service object that calls the procedure:
Code:
@Override
public void callProc(long id) {
MyDaoImpl dao = new MyDaoImpl();
String result = dao.callProc(leadId);
if (!result.equals("OK")) {
sendFailureMessage(leadId, result);
}
}
Log snippets:
Code:
INFO : com.mydomain.myapp.dao.myDaoImpl - setDataSource() called with org.apache.commons.dbcp.BasicDataSource@41c7d56b
...
INFO : com.mydomain.myapp.dao.myDaoImpl - dataSource is NULL!
...
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/MyApp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.mydomain.myapp.dao.StoredProcedureMyDao.<init>(StoredProcedureMyDao.java:32)
at com.mydomain.myapp.dao.MyDaoImpl.callProc(MyDaoImpl.java:69)
at com.mydomain.myapp.service.ServiceManagerImpl.callProc(ServiceManagerImpl.java:70)
at com.mydomain.myapp.controller.NavigationController.doProc(NavigationController.java:202)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:84)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)