DataSource and Factories

2001-07-25 Thread Andreas Leitner


Hi again,

I would like to seperate my buisness logic from the data layer. Say I
have an object Person, that has the fields id, firstName and lastName.
The only constructor I provide is one which takes two arguments (id,
firstName and lastName). The person objects will be stored in a certain
Database, but this may change in the future, this is why I use a
factory. The factory takes an id, connects to the DB, retrieves the
data, creates the Person object and returns it. 

This pattern also comes in handy, when you have classes that inherit
from Person. If the DB access would happen in the constructor, one could
only create Person objects, with a factory, you can create an object of
the real type instead.

Anyway, what I want is to get a DataSource without knowing about a
servlet. Is this somehow possible? Is there some static access to the
DataSource Repository?

Tia,
Andreas




Re: DataSource and Factories

2001-07-25 Thread Oleg V Alexeev

Hello Andreas,

Wednesday, July 25, 2001, 1:59:50 PM, you wrote:


AL> Hi again,

AL> I would like to seperate my buisness logic from the data layer. Say I
AL> have an object Person, that has the fields id, firstName and lastName.
AL> The only constructor I provide is one which takes two arguments (id,
AL> firstName and lastName). The person objects will be stored in a certain
AL> Database, but this may change in the future, this is why I use a
AL> factory. The factory takes an id, connects to the DB, retrieves the
AL> data, creates the Person object and returns it. 

AL> This pattern also comes in handy, when you have classes that inherit
AL> from Person. If the DB access would happen in the constructor, one could
AL> only create Person objects, with a factory, you can create an object of
AL> the real type instead.

AL> Anyway, what I want is to get a DataSource without knowing about a
AL> servlet. Is this somehow possible? Is there some static access to the
AL> DataSource Repository?

Take a look to the Castor project -
http://castor.exolab.org

-- 
Best regards,
 Olegmailto:[EMAIL PROTECTED]





Re: DataSource and Factories

2001-07-25 Thread suhas

DataSource can be obtained either Deployment descriptor as
"java:comp/jdbc/MyDataSource"
(associate a Jndi name to the Datasource
)


- Original Message -
From: Andreas Leitner <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; 'Aapo Laakkonen'
<[EMAIL PROTECTED]>
Sent: Wednesday, July 25, 2001 3:29 PM
Subject: DataSource and Factories


>
> Hi again,
>
> I would like to seperate my buisness logic from the data layer. Say I
> have an object Person, that has the fields id, firstName and lastName.
> The only constructor I provide is one which takes two arguments (id,
> firstName and lastName). The person objects will be stored in a certain
> Database, but this may change in the future, this is why I use a
> factory. The factory takes an id, connects to the DB, retrieves the
> data, creates the Person object and returns it.
>
> This pattern also comes in handy, when you have classes that inherit
> from Person. If the DB access would happen in the constructor, one could
> only create Person objects, with a factory, you can create an object of
> the real type instead.
>
> Anyway, what I want is to get a DataSource without knowing about a
> servlet. Is this somehow possible? Is there some static access to the
> DataSource Repository?
>
> Tia,
> Andreas
>




Re: DataSource and Factories

2001-07-25 Thread Bud Gibson

This looks interesting, but I am not sure how I would use it.

Would the strategy be to set up an action to run when the application is 
loaded?  Would the action then obtain a reference to the data source and 
stow it in some repository using JNDI for use by the persistenc layer?

That is the solution we think will work.  Is this different from that?

Thanks,
Bud

>DataSource can be obtained either Deployment descriptor as
>"java:comp/jdbc/MyDataSource"
>(associate a Jndi name to the Datasource
>)
>




AW: DataSource and Factories

2001-07-25 Thread Andreas Leitner



> Take a look to the Castor project -
> http://castor.exolab.org

That does indeed look awesome! Do you have experience with it? Is it
ready for production use? Many thanks for that great hint!


Andreas




Re: DataSource and Factories

2001-08-05 Thread Craig R. McClanahan



On Wed, 25 Jul 2001, Bud Gibson wrote:

> This looks interesting, but I am not sure how I would use it.
> 
> Would the strategy be to set up an action to run when the application is 
> loaded?  Would the action then obtain a reference to the data source and 
> stow it in some repository using JNDI for use by the persistenc layer?
> 
> That is the solution we think will work.  Is this different from that?
> 
> Thanks,
> Bud
> 
> >DataSource can be obtained either Deployment descriptor as
> >"java:comp/jdbc/MyDataSource"
> >(associate a Jndi name to the Datasource
> >)
> >
> 
> 

J2EE-based application servers (and some servlet containers, like Tomcat
4.0) support exactly this approach (JNDI-based access to data sources).  
The idea is that you identify (in your web.xml file) a logical name for
the database connection pool you want:

  
My Database
jdbc/MyDataSource
javax.sql.DataSource
Container
Shareable
  

Somewhere in the administration tools for your server, the system
administrator can set up a connection between this logical name and a
"real" database -- typically, you can set up parameters similar to what
are in the Struts  element.

Now, in a servlet or a class called by your servlet, you can access the
connection pool like this:

  import java.sql.Connection;
  import javax.naming.InitialContext;
  import javax.sql.DataSource;

  ...

  InitialContext ic = new InitialContext();
  DataSource ds =
(DataSource) ic.lookup("java:comp/env/jdbc/MyDataSource");
  Connection conn = ds.getConnection();
  ... use this connection ...
  conn.close();  // Returns connection to the pool

As you can see, there's no need for any references to the servlet layer at
all -- this works because the naming context is set up correctly by the
container.

Craig McClanahan