• Post Reply Bookmark Topic Watch Topic
  • New Topic

dataSource.getConnection()

 
Mustafa BOLUKBAS
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use jasperreports and i want to set a connection parameter to the report.
i use the code below.

public ModelAndView handleSubReport(HttpServletRequest request,
HttpServletResponse response) throws Exception {

Connection conn= dataSource.getConnection();
try
{
String uri = request.getRequestURI();
String format = uri.substring(uri.lastIndexOf(".") + 1);
Map model = new HashMap();
model.put("REPORT_CONNECTION", conn);
model.put("format", format);
return new ModelAndView("subReport", model);
}
catch(Exception e)
{
throw new Exception (e.getMessage());
}
finally
{
conn.close();
}
}

As you see i get the connection. And when i'm done with it. i send it
back to the pool. Actually i'm using hibernate in my project. But i
need to get the connection to make a subreport.

Here is my question.
Is it ok with spring what i'm doing with the connection?

I'm appriciated if you answer this as soon as possible.

Thanks in advance,
Mustafa.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mustafa,

The fact that you return your connection to your presentation layer is giving me a very uneasy feeling. From a design standpoint this is a very, very bad practice. If I were you I would design the Spring controller to return the data you need, rather than the physical database connection. Since you use Hibernate as well, it should be very easy for your code to return the list of DTOs (this is mostly an EJB term; Hibernate on the other hand doesn�t use this pattern and it deals with domain model business objects) to be displayed. Actually the Spring bind tag was specially designed for this purpose.
Another really bad thing with your design is that you return the connection to the pool. Basically the pool manger will mark that connection as available and another client could reserve it at any time. At the same time it could be used (reserved?!) by the first client. This will make your code extremely unstable and error prone.
Regards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!