Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which approach is correct for getting a connection of database from a business class  RSS feed

 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which approach is right for connecting to a database is a business class and why ?

Approach 1.

I want to know , whether we should ONLY call getDataSource() method in the main action class and pass the DataSource reference to any class wherever database connection is required ?


Approach 2.

In any Business class, if we want a connection to database then get the request object from main action class and and make the business class extend ACTION and call the getDataSource() method ?

I have used both approaches ,Firs approach works fine for me.
But second approach threw a null pointer exception .

What I want to know is whether second approach is right and technically correct to make a business class extend ACTION class and call the getDataSource() method ?


Eagerly waiting for a response !!


Response
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaan,
Your business class should NOT be extending Action as it is not an action.

I would prefer "none of the above". It would be better for the business class to get the datasource on it's own - through the JNDI or a connection pool/datasource - rather than passing it around. But passing it around is better than compromising the design of a business class by pretending it is an Action.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shaan patil:
But second approach threw a null pointer exception .

As it should. It's not enough to have an Action class. You need to have the action class that Struts is currently executing as this is where Struts set the datasource. Everywhere else it is null.
 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky ,

First of all Happy Holi(festival of colour ) from India, Thanks for the response.


Referring the discussions from thread ,


Can we conclude that, If we are not using JNDI for datasource access ,The only way to get a DataSource is through the method getDataSourrce in the action class(The one mapped in the struts-confi.xml).So That means there exists a DataSource per Action class which is configured in the struts-config.xml. Am i right ?


Regards
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this thread for a discussion of these same issues.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!