> (1) Is it best for the data access layer to throw exceptions on error to
> be picked up by the business objects & then struts actions, or would it
> be good to use a struts-type message object and use strings in the
> application resources file (I'm just worried my exceptions' text would
> not be multi-lingual)

In the event of a real emergency, the data access layer should throw
exceptions, which your business layer can catch. I find chaining
exceptions to be very effective. 

http://www.javaworld.com/javaworld/jw-08-2001/jw-0803-exceptions.html

You can add your own business explanationa at the top of the chain and
show both the business explantion and the actual cause. The business
explanation calms the mere mortals and the actual cause helps tech
support with problem determination. 

The Struts message system builds on the standard Java ResourceBundle
framework. So I would say that it is perfectly valid for your business 
objects to return tokens from the ApplicationResource file. This file 
can be used in any application and is not Struts-specific. (Especially 
if you are using the message tags from Struts 1.1b or 1.0 version of 
the Validator.)

If you really want to hedge your bets, write your own business layer
first. This layer takes the input each business operation requires and
hands back a Collection, Object, or maybe just a status code.
Internally, the business layer takes the input and connects to the data
access layer (JDBC, Castor, Turbine, whatever). This can also let you
chain the exceptions or do whatever other handling you need to make.
Your business layer becomes bound to the data access layer but can be
changed without touching the web tier. 

This also makes it easy to use multiple data access devices. You might
need to use Castor for some things and Lucene for others. The business
layer can provide a consistent interface to the application platform
(Struts, Swing, whatever), and then choose whatever source it wants to
use for data. This would also include manufacturing data for testing or
debugging.

Chapter 12 and 13 of Professional JSP Site Design from Wrox show using 
this approach for JDBC and then integrating Lucene for some of the 
searches. Struts is used by the example application here, as it is 
throughout most of the book. 

http://jakarta.apache.org/struts/resources.html#books
(4th from the top)

The Action's role is to bundle the input from the ActionForm bean into
whatever input the business layer expects. The Action then either
forward the result along or cope with any error condition.


> (2) How would I get the database connection info to the data access
> layer? Presuming I want to set it up in struts-config.xml or as in
> commons-dbcp, would I then pass it in from struts actions into the
> business object and then into the data access layer as a parameter on
> each call?


As mentioned elsewhere, getting a database connection from the web tier
is not a good practice. The Struts framework does try to encourage best
practice but sometimes takes a dip in the pool of pragmatism. The
Generic Connection pool is one example of this. The data access objects
should take care of all the connection stuff and just pass back standard
Java objects like JavaBeans and Collections. The web tier, and even
business tier, does not need to know where the data comes from. 

-- Ted Husted, Husted dot Com, Fairport NY US
-- Developing Java Web Applications with Struts
-- Tel: +1 585 737-3463
-- Web: http://husted.com/about/services


Adam Hardy wrote:
> 
> Hi All,
> 
> I saw this thread in the archives and I thought I'd pinch the title.
> I've spent this afternoon surfing the web looking at stuff on Sun,
> Jakarta  and lots of smaller sites looking for a ready-made data access
> layer package but I take it there isn't an open-source one out there.
> 
> Obviously I don't want to tie my data access layer to struts, so there
> are a few questions I've got.
> 
> (1) Is it best for the data access layer to throw exceptions on error to
> be picked up by the business objects & then struts actions, or would it
> be good to use a struts-type message object and use strings in the
> application resources file (I'm just worried my exceptions' text would
> not be multi-lingual)
> 
> (2) How would I get the database connection info to the data access
> layer? Presuming I want to set it up in struts-config.xml or as in
> commons-dbcp, would I then pass it in from struts actions into the
> business object and then into the data access layer as a parameter on
> each call?
> 
> I've seen quite a few patterns like www.martinfowler.com and code like
> in Sun's Pet Store, so I think I've got a fairly good idea of what I
> need. The data access layer I want has got to:
> 
> - take as input and return as output Data Transport Objects
> - allow the business objects to control transactions
> - use a connection pool
> - possibly keep the SQL statements in an external XML file
> - have comprehensive error handling & reporting
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to