Board index » delphi » Instantiate a Form with DB components

Instantiate a Form with DB components

I have a Main form with a TDBGrid using the dataset of a 'Name' table.  I
also have an Edit form with various TDBEdit, TDBMemo, etc components for
a single record from the 'Name' table.  The application is a single thread.

I would like to be able to instantiate the Edit form any number of times
for different records from the 'Name' table.  There are certain issues
with components and good design practice...

1a. Should the TTable, TQuery, TDataSource, etc., normally in a TDm
    be included with the Edit form?

1b. Does it make sense to create a separate TDm for the Edit form which
    gets instantiated when the Edit form is instantiated?

2. Would I need a TDatabase component for each Edit form instantiated,
   especially since I want to be able to Rollback transactions?

3. Since it's a single threaded program, do I only need the default session?

TIA

-bruceh-

 

Re:Instantiate a Form with DB components


Quote
"Bruceh" <br...@stillnotmyemailaddr.net> wrote in message

news:3BA93557.4E9DEDD6@stillnotmyemailaddr.net...

Quote
> I have a Main form with a TDBGrid using the dataset of a 'Name' table.  I
> also have an Edit form with various TDBEdit, TDBMemo, etc components for
> a single record from the 'Name' table.  The application is a single
thread.

> I would like to be able to instantiate the Edit form any number of times
> for different records from the 'Name' table.  There are certain issues
> with components and good design practice...

> 1a. Should the TTable, TQuery, TDataSource, etc., normally in a TDm
>     be included with the Edit form?

If it is a simple editing form (1 or 2 tables at most) then I would
put the components right on the form. I would use live queries
so you only pull the record you want to edit.

Quote

> 1b. Does it make sense to create a separate TDm for the Edit form which
>     gets instantiated when the Edit form is instantiated?

If you use datamodules instead of putting the components on the form,
then yes, you should have each instance of the form create it's own
datamodule so there is no interference.

Quote

> 2. Would I need a TDatabase component for each Edit form instantiated,
>    especially since I want to be able to Rollback transactions?

No matter which way you go, one database connection for the
application itself is sufficient. Transactions are based on a group
of actions, not a database connection. Many transaction can
exist at the same time through one database connection.

Quote

> 3. Since it's a single threaded program, do I only need the default
session?

Yes.

HTH
Woody

Re:Instantiate a Form with DB components


Quote
Woody wrote:
> "Bruceh" <br...@stillnotmyemailaddr.net> wrote in message
> news:3BA93557.4E9DEDD6@stillnotmyemailaddr.net...>
> > 2. Would I need a TDatabase component for each Edit form instantiated,
> >    especially since I want to be able to Rollback transactions?

> No matter which way you go, one database connection for the
> application itself is sufficient. Transactions are based on a group
> of actions, not a database connection. Many transaction can
> exist at the same time through one database connection.

I'm confused.  If there is a single TDatabase component and
my Edit form's Show() module has a local NameTable:
    Dm->MyTDatabase->StartTransaction();
    NameTable->Edit();
    <...stuff...>

and the OnClose() event would contain:
    NameTable->Post();
    if (errorFlag == true)
        Dm->MyTDatabase->Rollback();
    else
        Dm->MyTDatabase->Commit();

If I instantiate the Edit form twice, there would be two StartTransaction()s
pending.  With this being a single threaded program how would
MyTDatabase know which Rollback or Commit that it's dealing with?

Thanks....

-bruceh-

Re:Instantiate a Form with DB components


Quote
"Bruceh" <br...@nothere.net> wrote in message

news:3BAAB1BE.C7F3799@nothere.net...

Quote

> I'm confused.  If there is a single TDatabase component and
> my Edit form's Show() module has a local NameTable:
>     Dm->MyTDatabase->StartTransaction();
>     NameTable->Edit();
>     <...stuff...>

> and the OnClose() event would contain:
>     NameTable->Post();
>     if (errorFlag == true)
>         Dm->MyTDatabase->Rollback();
>     else
>         Dm->MyTDatabase->Commit();

> If I instantiate the Edit form twice, there would be two
StartTransaction()s
> pending.  With this being a single threaded program how would
> MyTDatabase know which Rollback or Commit that it's dealing with?

The way you are doing this, you will indeed need separate TDatabase
components. You should not be starting a transaction as soon as the form
opens. Transactions should never be left open for a long time and this means
they should not be left open at the control of the user since they could
walk away.

You can place the dataset into Edit mode without starting a transaction,
editing happens locally in the dataset. Only when you are ready to post do
you need to start a transaction, change your close event to something like:

     Dm->MyTDatabase->StartTransaction();
     try;
        NameTable->Post();
        Dm->MyTDatabase->Commit();
     catch;
        Dm->MyTDatabase->Rollback();
     end;

By doing this, any number of instances can share a TDatabase.

--
Wayne Niddery (Logic Fundamentals, Inc.)
RADBooks: http://www.logicfundamentals.com/RADBooks/delphibooks.html
"Some see private enterprise as a predatory target to be shot, others as a
cow to be milked, but few are those who see it as a sturdy horse pulling the
wagon." - Winston Churchill

Re:Instantiate a Form with DB components


Ahh... So transactions need to only wrap around the Post's and not
the Edit's.  Now I understand....

Thanks all...

-bruceh-

Quote
> You can place the dataset into Edit mode without starting a transaction,
> editing happens locally in the dataset. Only when you are ready to post do
> you need to start a transaction, change your close event to something like:

>      Dm->MyTDatabase->StartTransaction();
>      try;
>         NameTable->Post();
>         Dm->MyTDatabase->Commit();
>      catch;
>         Dm->MyTDatabase->Rollback();
>      end;

> By doing this, any number of instances can share a TDatabase.

Re:Instantiate a Form with DB components


"Wayne Niddery [TeamB]" <wnidd...@aci.on.ca> wrote in message
news:3babc62e$1_2@dnews...

Quote
> "Bruceh" <br...@nothere.net> wrote in message
> news:3BAAB1BE.C7F3799@nothere.net...

> The way you are doing this, you will indeed need separate TDatabase
> components. You should not be starting a transaction as soon as the form
> opens. Transactions should never be left open for a long time and this
means
> they should not be left open at the control of the user since they could
> walk away.

This is only true for TClientDataSets or (I assume) when using the BDE with
certain databases. For any RDBMS like Interbase, a transaction needs to be
open to do anything, including viewing the data without editing it.

In my case, I only use 1 database connection but each form (or DM) has it's
own transaction components. That was the type of theory I was trying to
explain but didn't do a very good job. :)

Woody

Re:Instantiate a Form with DB components


Quote
> This is only true for TClientDataSets or (I assume) when using the BDE with
> certain databases. For any RDBMS like Interbase, a transaction needs to be
> open to do anything, including viewing the data without editing it.

> In my case, I only use 1 database connection but each form (or DM) has it's
> own transaction components. That was the type of theory I was trying to
> explain but didn't do a very good job. :)

Well, currently I'm using BDE, but I may want to use Interbase later.  So
with Interbase, if I instantiate a Form twice and both are Editing different
records from the same database table, if I want transactions, I would need
a separate TDatabase component for each form so I can call StartTransaction.
Yes?

-bruceh-

Other Threads