datasource issue

Micheal_Male
Micheal_Male used Ask the Experts™
on
So i have an applicationContext.xml which has a bean dataSource defined in it.

<bean id="dataSource" class="org.apache.commons.....">
<propertyName="driverClassName">
<value>mysql....</value>
</property>
.
.
.
.
</bean>

<bean id="testDao" class="com.testDao">
<property name="dataSource" ref="dataSource"/>
</bean>

Now in my testDao i had the dataSource :-

private DataSource dataSource;

setters and getter for the datasource. Now in one of my method when i try to get connection from the dataSource it's always coming null. Any clue why is it doing that ?

Connection conn = null;
conn = getDataSource().getConnection();

I get a nullpointer exception in this statement, hence my queries don't get executed. Can anyone tell me how to get the connection instance from the dataSource ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Are you getting any messages in the Spring logs about not bean able to instantiate your dataSource bean (and hence it being null when injected into your testDao)?

Also, is your testDao setter and getter implemented properly? (I know this might sound simple but just wanted to check. Can you post that code?)

As an aside, are those applicationContext.xml lines that you have posted a direct cut and paste from the file or have you retyped them? The second line should be ...

<property name="driverClassName">

Author

Commented:
I just posted bits and pieces of it. The datasource is cofigured properly meaning driverClassName, url, username, password, etc are all cofiured properly in applicationContext.xml. i am not even getting any errors also when restarting my tomcat. So when i tried to access the webapp at that point when that method is executed i get an error. When printing the stacktrace and putting the tomcat in debug mode i saw that conn is null. it does not get any connection from the datasource.

I also tried to connect to the DB url manually meaning mysql client and it worked fine. So dataSource.getConnection() should make a connection to my DB correct ?. I've always used JdbcTemplate and never encountered any issue but this is the first time i am trying to get the connection explicity from the datasource and getting errors. The app does not even print the error but when i printed the stacktrace it states null for conn.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
The fact that conn is null is not your problem here (unless you haven't described the problem properly). You stated in your original question that you get the null pointer exception at the line...

conn = getDataSource().getConnection();

The only way that you could get a null pointer exception there is if getDataSource() is returning a null, ie. nothing to do with conn or getConnection() returning a null. For getDataSource() to return a null, either it isn't being returned correctly in your getDataSource() method, or it isn't being set correctly in your setDataSource(DataSource dataSource) method or spring failed to create an actual DataSource object to inject into your testDao.

It really is a bit hard to help you any more than that without you posting more of the code, or the actual stacktrace, etc.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
I totally understand your point. My question is that all i am doing is using the regular setter and getter method of dataSource().

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.setDataSource = dataSource;
}

I also put an init method to set the dataSource.

public void init() {
Validate.notNull(this.dataSource,"No Empty DataSource")
}

and in my bean :-

<bean id="testDao" class="com.testDao" init-method="init">
<property name="dataSource" ref="dataSource"/>
</bean>

this will atleast throw an exception at compile time when restarting the server. However, nothing happens. Am i missing something over here ?  Do i need to set the connection explicity in getDataSource() ?. I thought the dataSource is being injected by spring in applicationContext and it is available in my dao.  The exception is nothing but NullPointerException at that statement.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Again, did you retype that code, because your setter is wrong (as it appears in your post). It should be...

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

But it shouldn't have compiled as it was...

Author

Commented:
Yup that was a typo.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
You can't post the stack trace?

Author

Commented:
Test Connection now
java.lang.NullPointerException
      at com.testDao.<init>(TestDao.java:164)
      at com.testDao.saveUser(TestDao:263)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:183)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:138)
      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:175)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      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:286)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
      at java.lang.Thread.run(Unknown Source)

Author

Commented:
I also passed some dummy values in the init method of the Dao. Just wanted to see if i get the datasource empty or not and i DID NOT. It printed the connection and it even executed the query also. But when i call it through my application the connection comes null.
You cant directly create the instances(setter method) of dataSources in ur class.
this entry must be in ur  dispatcher-serlet.xml flie
<bean id="dataSource" class="org.apache.commons.....">
<propertyName="driverClassName">
<value>mysql....</value>
</property>
.
.
.
.
</bean>
 
you must extend jdbcTemplate and HibernateTemplate
Thats wiring pblm .
extend the class use any one the method ...
if u need to acces menas ...
do like this ...
<bean id="myDataSource" class="org.apache.commons.....">
<propertyName="driverClassName">
<value>mysql....</value>
</property>
 
<bean id="testDao" class="com.testDao">
<property name="dataSource" ref="myDataSource"/>
</bean>
 
in setter methodd u can write like this
 
 DataSource dataSource;
 SimpleJdbcTemplate jdbcTemplate;

   public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

 
 
 
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
That stack trace is showing that the exception is happening in the TestDao contructor. Are you creating a new TestDao object in your TestDao.saveUser() method? (That is what the stack trace is indicating)

If you create a new instance of TestDao then Spring has no way of injecting your dataSource into that new instance. Spring will only inject dependencies into the instances that it is asked to create via the xml not instances that you create in your code. There looks like there is something structural with what you are trying to do that ins't quite right. Are you able to post your full TestDao.java file or is there stuff in there that you can't share?

Author

Commented:
you are correct. I am creating a new instance which is where i am trying to get the dataSource connection but always receive null. I understand the concept now, and you are right. Spring does everything through xml which is why my datasource is null.

Trying to see if there is any other way i can inject through xmL so it remembers the datasource.

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
I guess the question is: why do you need to create a new instance of the TestDao object? Why can't you use the one that Spring created for you?

(BTW: I am off on holidays now for 4 days so I will see how you are going when I am back!)

Author

Commented:
I actually got a temp solution. I read the datasource.xml in my java program and created a custom Connection class to utilize my queries. In old days they were creating objects as spring was not there at that time. So i wanted a temporary solution to work. Thanks carl for your help.

Author

Commented:
thank you for answering my questions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial