Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Obtain connection in DAO implementation

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have more or less based this code on the JDBC sample programs from the official site. Here my aim is to write a DAO for fetching a list on some predefined condition.

So I will write something that will be of the following form:



The doubts I have can be listed as:

- Does the connection to the db performed correctly?

- Do I need a separate connection pool from where I can grab a connection (keeping in mind this is a small application) ?

- Do libraries like DBUtils really make life a breeze?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- It's not a good idea to establish a new connection or transaction in every DAO method, in fact it's better not to let the DAO be responsible for creating a connection / transaction at all. Although it is possible to combine such a solution with connection pooling, you will run into problems with transaction scoping. Often a single transaction will need to span invocations of multiple methods across multiple DAOs, which will become impossible if each DAO method establishes a new connection. A JDBC DAO should be treat a Connection as a dependency, which could be instance bound or thread bound, whatever you need.
- Connection pooling is probably always worth it.
- Things like Apache's DBUtils or Spring's JdbcTemplate can help you immensely with avoiding some of the boiler plate code and solving other issues, but there are learning curves associated with their proper use, especially in the case of the Spring framework.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate the given suggestions; I will take an effort to incorporate these points when improving the program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic