DAOs and Connection sharing

From: janne (janne_1976_at_hotmail.com)
Date: 07/30/04


Date: 30 Jul 2004 01:06:24 -0700

I have been using a DAO -based design to implement my applications
with the following principles:

- business / persistence logic is handled by 3 layers; Actions,
business logic classes and DAOs
- logic in a use case is handled by an Action (Struts is not used; but
for the sake of discussion you might as well assume it is)
- Action gets a Connection from a DataSource; and passes it to each
business logic class it uses
- When persistence is needed, business logic class calls a DAO and
passes it the Connection. All database activities therefore share the
same connection and transaction.
- After Action is finishing, Action either commits (if no exceptions
were thrown) or rolls back (in case of exceptions) the connection, and
closes it.

This works fine, but there's coupling between
actions-business-persistence with the passed Connections. I have seen
suggestions that an alternative approach could be used:

- Connection objects are not passed as method arguments. Instead each
DAO fetches Connections from DataSource directly
- I would still use Action to commit / rollback the whole transaction

The basis for this, according to some, is that when different objects
in same thread request connections from a datasource, they in fact get
the same shared connection. Which is a requirement for this to work,
of course.

Does this work, and if it does, can you point to a specification that
describes DataSource's correct behaviour? I have not been able to find
any.

Hibernate and other alternatives are all very nice, but I want to
concentrate on this kind of approach now...



Relevant Pages

  • Re: Java DAO pattern: singleton and threadsafe?
    ... There's one abstract DAO ... That the DAOs are singletons and that there is one abstract superclass are orthogonal. ... I think a different connection should be gotten from the datasource ...
    (comp.lang.java.programmer)
  • Re: Strange Problems with ODBC connection to SQL Server
    ... When accessing Jet data, use DAO. ... This way you are not exposing connection information in clear ... >accesses an SQL Server 2000 repository via ODBC data source. ...
    (microsoft.public.data.odbc)
  • Re: Best practice - Connected/Disconnected mode - and more
    ... The implication here is your data layer is considered a service that your business layer uses. ... This leads people to ORM tools, of which LINQ is not one, no matter what people sell it at. ... This is due to many factors, including the memory on the database server to maintain the connection, as well as the expense of locking, etc. ... Does this mean a strongly typed Dataset? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: two PCs - two locations
    ... > to his business location about a tenth of a mile away. ... He said the two PCs ... not sure what sort of connection he might have, but it is probably not very ... Opening an Access database over a "loose" connection is a quick way to ...
    (comp.lang.basic.visual.misc)
  • Re: security
    ... She shares a broadband connection with another small business in an adjacent room, using a wireless connection to a router. ... If her PC is recent, up to date with patches, properly set up and locked down with a decent firewall in place it should be reasonably secure for normal purposes. ... When they quote for sorting the problem, get a second quote from someone else, and some references from satisfied customers. ...
    (uk.telecom.broadband)