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:[EMAIL PROTECTED]]
> Sent: Thursday, March 08, 2001 11:19 PM
> To: [EMAIL PROTECTED]
> 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
>
>

Reply via email to