You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael McCallister <r2...@email.sps.mot.com> on 2001/03/08 23:19:22 UTC

Business Object Bean Persistence Strategies

What strategies or patterns do people use to manage persistence of business 
object data in a Struts application when there is no EJB layer and there is 
a desire to keep the business objects as independent of the web portion of 
the application as possible?  Do you use the Struts DataSource and pass 
either it or a Connection as a parameter to bean methods that take 
responsibility for managing persistence?  Do you follow the J2EE blueprint 
and create separate Data Access Objects to support persistence?  Are you 
using an open-source framework to manage persistence?  This seems like a 
common problem, but I haven't seen much talk about common solutions.


Mike


Re: Business Object Bean Persistence Strategies

Posted by Jim Richards <gr...@cyber4.org>.
My approach is to have:

JSP	<->	Struts Form/Action	<->	Business Rules	<->	Database

The busines rules are set up as singleton object on servlet
startup. I've subclassed the ActionServlet and overloaded
the datasource init method, there I set up the four business
rule classes and put them into application scope, on
creation the business classes get their datasource reference.

The objects I pass around are data beans that model the database
closely (similar to entity EJBs). 

So for logon, I have a User() object where I set the
user_id and password from the logon form, then the action
class will call User user = UserManager.logon(User user)
which will either find the user and return a fully filled
user object or throw and exception (I might change that though).

UserManager has other methods for logoff, update and create. So the
action code doesn't need to send the dataconnection because
that ties the Action too closely to the data store (what happens
if the authentication changes to LDAP or unix password/username
or database password/username?).

It also means I can reuse the business rules for another project
that might not be struts based. (But whay would I do that ;-)


Michael McCallister wrote:
> 
> What strategies or patterns do people use to manage persistence of business
> object data in a Struts application when there is no EJB layer and there is
> a desire to keep the business objects as independent of the web portion of
> the application as possible?  Do you use the Struts DataSource and pass
> either it or a Connection as a parameter to bean methods that take
> responsibility for managing persistence?  Do you follow the J2EE blueprint
> and create separate Data Access Objects to support persistence?  Are you
> using an open-source framework to manage persistence?  This seems like a
> common problem, but I haven't seen much talk about common solutions.
> 
> Mike

Re: Tag Examples

Posted by Marty Fisher <mf...@systemslink.com>.
That did not work but thank you for reponding.  I was still able to modify the input
data. Do you have a site that explains the style attribute and contains some examples ?

Thanks again
Marty

Kartik Sudarsan wrote:

> Hi,
>
>         This is in response to Marty's query (part query that is), i think we can
> disable input in the struts text tag by specifying it in the style attribute
> in the following manner
>
>         <html:text property="name" size="20" maxlength="80" style="disabled:none"/>
>
> i hope this solves your problem.
>
> Regards,
>
> Kartik Sudarsan
>
> IT Cube India Pvt. Ltd,
> Unit 1, 8th Floor,
> Innovator Building
> ITPL, Whitefield Road
>
> A BOOK IS A MIRROR :IF AN ASS PEERS INTO IT ,YOU  CAN'T EXPECT AN APOSTLE TO
> LOOK OUT
> -----Original Message-----
> From: mmuchnik@jakarta.apache.org [mailto:mmuchnik@jakarta.apache.org]On
> Behalf Of Maya Muchnik
> Sent: Friday, March 09, 2001 7:48 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Tag Examples
>
> Hi,
>
> I would like to ask a question additional to Marty Q. Is it possible with
> "html:form" tag (I am not sure that it is correct tag) to disable change
> (input) in
> some fields when data are restored and displayed in the form. The 2nd Q: is
> it
> possible to display default values for some fields when no data were read -
> a new
> form. Maybe it will in struts 1.1?
>
> Thanks in advance.
>
> Maya
>
> Marty Fisher wrote:
>
> > Hello,
> >
> > We would like to add a attribute to a form tag.  In the case of the text
> tag, we
> > need to disable the form input from being editted yet display its value.
> We also
> > need a max length attribute on the text area tag.
> >
> > Can someone direct me to some jsp/servlet examples to extending the strut
> tags ?
> > Thanks
> > --
> > Marty Fisher
> > Cranbury
> > 609-409-0909
> > HNC Software, Inc. (formerly Systems/Link Corporation)

--
Marty Fisher
Cranbury
609-409-0909
HNC Software, Inc. (formerly Systems/Link Corporation)

The information contained in this transmission is privileged and/or confidential
information intended for the use of the individual(s) or entity named above. If
the reader of this message is not the intended recipient, you are hereby notified
that any dissemination, distribution, or copying of this communication is strictly
prohibited.



RE: Tag Examples

Posted by Kartik Sudarsan <ka...@itcube.net>.
Hi,

	This is in response to Marty's query (part query that is), i think we can
disable input in the struts text tag by specifying it in the style attribute
in the following manner

	<html:text property="name" size="20" maxlength="80" style="disabled:none"/>

i hope this solves your problem.

Regards,

Kartik Sudarsan

IT Cube India Pvt. Ltd,
Unit 1, 8th Floor,
Innovator Building
ITPL, Whitefield Road

A BOOK IS A MIRROR :IF AN ASS PEERS INTO IT ,YOU  CAN'T EXPECT AN APOSTLE TO
LOOK OUT
-----Original Message-----
From: mmuchnik@jakarta.apache.org [mailto:mmuchnik@jakarta.apache.org]On
Behalf Of Maya Muchnik
Sent: Friday, March 09, 2001 7:48 PM
To: struts-user@jakarta.apache.org
Subject: Re: Tag Examples


Hi,

I would like to ask a question additional to Marty Q. Is it possible with
"html:form" tag (I am not sure that it is correct tag) to disable change
(input) in
some fields when data are restored and displayed in the form. The 2nd Q: is
it
possible to display default values for some fields when no data were read -
a new
form. Maybe it will in struts 1.1?

Thanks in advance.

Maya

Marty Fisher wrote:

> Hello,
>
> We would like to add a attribute to a form tag.  In the case of the text
tag, we
> need to disable the form input from being editted yet display its value.
We also
> need a max length attribute on the text area tag.
>
> Can someone direct me to some jsp/servlet examples to extending the strut
tags ?
> Thanks
> --
> Marty Fisher
> Cranbury
> 609-409-0909
> HNC Software, Inc. (formerly Systems/Link Corporation)



Re: Tag Examples

Posted by Maya Muchnik <mm...@pumatech.com>.
Hi,

I would like to ask a question additional to Marty Q. Is it possible with
"html:form" tag (I am not sure that it is correct tag) to disable change (input) in
some fields when data are restored and displayed in the form. The 2nd Q: is it
possible to display default values for some fields when no data were read - a new
form. Maybe it will in struts 1.1?

Thanks in advance.

Maya

Marty Fisher wrote:

> Hello,
>
> We would like to add a attribute to a form tag.  In the case of the text tag, we
> need to disable the form input from being editted yet display its value.  We also
> need a max length attribute on the text area tag.
>
> Can someone direct me to some jsp/servlet examples to extending the strut tags ?
> Thanks
> --
> Marty Fisher
> Cranbury
> 609-409-0909
> HNC Software, Inc. (formerly Systems/Link Corporation)


Tag Examples

Posted by Marty Fisher <mf...@systemslink.com>.
Hello,

We would like to add a attribute to a form tag.  In the case of the text tag, we
need to disable the form input from being editted yet display its value.  We also
need a max length attribute on the text area tag.

Can someone direct me to some jsp/servlet examples to extending the strut tags ?
Thanks
--
Marty Fisher
Cranbury
609-409-0909
HNC Software, Inc. (formerly Systems/Link Corporation)




Re: Business Object Bean Persistence Strategies

Posted by Dan Malks <da...@sun.com>.
Hi Mike,
We just released a catalog of commonly used patterns and strategies to the
developer community. We touch on this issue among others.  ( For specific
discussion of this issue have a look at the View Helper pattern. There is a
sidebar titled Helpers about halfway down the page in the 'Solution' section
that includes the following:

So how does the Account object access the Business Services. Let
                       us examine two cases, one simple and the other more
                       sophisticated. In the more simple case, imagine that a
project is
                       taking a phased approach, phasing in EJB into the
Business Tier
                       over time. Assume at the moment that the database is
being
                       accessed via JDBC calls from the presentation tier. In
this case, the
                       Account object uses a Data Access Object (see Data
Access Object
                       pattern), hiding the underlying implementation details
of accessing
                       the database. The Data Access Object knows what SQL
queries are
                       necessary to retrieve the information. These details are
hidden from
                       the rest of the application, reducing coupling and
making each
                       component more modular and reusable. This case is
described in the
                       previous sequence diagram.

                       When the architecture becomes more sophisticated, and
EJB is
                       introduced in the Business Tier, then the Data Access
Object is
                       replaced with a Business Delegate (see Business Delegate
pattern),
                       typically written by the developers of the Business
Service. The
                       Delegate hides the implementation details of EJB lookup,

                       invocation, and exception handling from its client. It
might also
                       improve performance by providing caching services.
Again, the
                       object reduces coupling between tiers, improving the
reusability and
                       modularity of the various components. Regardless of the
specific
                       implementation of this object, its interface may remain
unchanged
                       during this transition. The following sequence diagram
describes
                       this scenario after the transition to the Business
Delegate)

The url is:
http://developer.java.sun.com/developer/restricted/patterns/ViewHelper.html

and requires JDC login.

Any feedback or general comments, positive or negative, on the pattern catalog
are much appreciated.

Hope this helps.
-dm

Michael McCallister wrote:

> What strategies or patterns do people use to manage persistence of business
> object data in a Struts application when there is no EJB layer and there is
> a desire to keep the business objects as independent of the web portion of
> the application as possible?  Do you use the Struts DataSource and pass
> either it or a Connection as a parameter to bean methods that take
> responsibility for managing persistence?  Do you follow the J2EE blueprint
> and create separate Data Access Objects to support persistence?  Are you
> using an open-source framework to manage persistence?  This seems like a
> common problem, but I haven't seen much talk about common solutions.
>
> Mike

--
Dan Malks                                 Sun Java Center
Enterprise Java Architect            703.208.5794



RE: Business Object Bean Persistence Strategies

Posted by Johan Compagner <jc...@j-com.nl>.
Hi,

Because i want Database Objects to control them self completely (like Enity
beans)
I must have a way to get Transaction support and make sure that in on thread
the same connection is being used. For example:

class Person
{
	private String name;
	private Country country;

	public static Person findByPrimaryKey(int i)
	{
		Connection connection = datasource.getConnection();
		// Select
		Person person = new Person();
		person.setName(rs.getString(1));
		person.setCountry(Countr.findByPrimaryKey(rs.getInt(2)));
		connection.close();
	}
}

class Country
{
	privatge String name

	public static Country findByPrimaryKey(int i)
	{
		// Now another connection is being taken from the datasource
		// if max == 2 then another call in this thread to the datasource
		// will present a deadlock!!
		Connection connection = datasource.getConnection();
		// Select
		Country country = new Country ();
		/// Filling
		connection.close()
		return country;
	}
}

So i want a datasource to return a connection that is already attached to
the
current thread.

Same goes for updating or inserting of data for example a new Person with a
new Country object that i want to insert into the database

person clas:
{
	public void store()
	{
		Connection connection = datasource.getConnection();
		// Before storing the country id the country must first be stored:
		country.store();
		// if all goes well:
		// Store the person (insert into person values(?,?,?,?)
		ps.setInt(2,country.getPrimaryKey());
		// if not all goes well the connection.rollback must be called.
		// but one problem the country was stored under a different connection
		// so it will not be rolledback!!
	}
}
country class
{
	public void store()
	{
		// Another connection is taken so if it goes bad then you can't rollback
		// everything!!
		Connection connection = datasource.getConnection();
		// Store the country (insert into person values(?,?,?,?)
	}
}

To get around those above problems i am building a Transaction class.
This wraps around the Datasource
it has a static method:

public static Transaction startTransaction() // Only starts a transaction
onces for the current thread.

and the Transaction object has methods:

public Connection getConnection()
public void setRollbackOnly() // Error occured somewhere so only a rollback
allowed.
public int getStatus() // Must rollback, Healty.
public void rollback()
public void commit() // Can only do this if mustRollback == false else throw
exception
public void stopTransaction()

In the above examples i don't do
	datasource.getConnection()
but
	Transaction transaction = Transaction.startTransaction();
	Connection connection = transaction.getConnection();
	// Cannot call connection.close() , connection.rollback(),
connection.commit() directly!!!
	transaction.stopTransaction();

If anyone is interrested or has some comments i am happy to hear it.

Johan

> -----Original Message-----
> From: Michael McCallister [mailto:r2126c@email.sps.mot.com]
> Sent: Thursday, March 08, 2001 11:19 PM
> To: struts-user@jakarta.apache.org
> Subject: Business Object Bean Persistence Strategies
>
>
> What strategies or patterns do people use to manage persistence
> of business
> object data in a Struts application when there is no EJB layer
> and there is
> a desire to keep the business objects as independent of the web
> portion of
> the application as possible?  Do you use the Struts DataSource and pass
> either it or a Connection as a parameter to bean methods that take
> responsibility for managing persistence?  Do you follow the J2EE
> blueprint
> and create separate Data Access Objects to support persistence?  Are you
> using an open-source framework to manage persistence?  This seems like a
> common problem, but I haven't seen much talk about common solutions.
>
>
> Mike
>
>