You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Petter Måhlén <pe...@chello.se> on 2004/02/14 13:46:10 UTC

When and where to do application initialisation?

Hi,

I'm trying to learn how to use Tapestry, and am putting together a very
basic application. I am right now trying to get a page that reads some data
from a database table to work, and to do that I of course need to ensure
that the database connections and stuff are set up properly. My question is
where I would best do that? I would prefer not having some kind of check if
the application is initialised every time a page is rendered. I'm looking
for something like this, I think:

1. servlet engine starts up
2. first request comes in
3. tapestry starts up
4. my application starts up - including configuration and startup of the
back-end
5. page is shown

My questions are:
1) where best to put initial configuration data (like database URL and links
to configuration files for the back-end, etc) - in the .application file
maybe? 
2) where to place a call to the initialisation method of the back-end?

Thanks,

/ Petter


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by pr...@prajnait.com.
I do 2 things.  1., I use the ThreadLocal pattern for the DB 
connection.  public static methods on a DB util class provide the 
connection object ... creating it lazily on first request from that 
Thread.

2.,  I subclass engine and override its service() method to make sure 
the connection is always closed at the end of the request.

My next steps: I'm adding SpringFramework to handle transaction 
management in a flexible, declarative (Aspect-Oriented) way.

HTH

== Ezra E.

Quoting Petter Måhlén <pe...@chello.se>:

> Hi,
> 
> I'm trying to learn how to use Tapestry, and am putting together a
> very
> basic application. I am right now trying to get a page that reads
> some data
> from a database table to work, and to do that I of course need to
> ensure
> that the database connections and stuff are set up properly. My
> question is
> where I would best do that? I would prefer not having some kind of
> check if
> the application is initialised every time a page is rendered. I'm
> looking
> for something like this, I think:
> 
> 1. servlet engine starts up
> 2. first request comes in
> 3. tapestry starts up
> 4. my application starts up - including configuration and startup of
> the
> back-end
> 5. page is shown
> 
> My questions are:
> 1) where best to put initial configuration data (like database URL
> and links
> to configuration files for the back-end, etc) - in the .application
> file
> maybe? 
> 2) where to place a call to the initialisation method of the
> back-end?
> 
> Thanks,
> 
> / Petter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tapestry-user-help@jakarta.apache.org
> 
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by pr...@prajnait.com.
Quoting "F. Da Costa" <da...@xs4all.nl>:

> Petter Måhlén wrote:
> 
> > Hi,
> > 
> > I'm trying to learn how to use Tapestry, and am putting together a
> very
> > basic application. I am right now trying to get a page that reads
> some data
> > from a database table to work, and to do that I of course need to
> ensure
> > that the database connections and stuff are set up properly. My
> question is
> > where I would best do that?
> Just create a Global object. It can be used by any class within
> Tapestry 
> (so its not visit depended).
> Just create it when you start up the app or do lazy instantiation.
> 
> Fermin DCG

If you do that, then any request can interact with the JDBC 
connection.  Now since requests can occur on multiple threads and JDBC 
connections are not thread-safe this will instantly break as soon as 
you have 2 simultaneous users.  In other words, you might be getting 
lucky testing stand-alone, but you cannot deploy such a system.  

This approach, if used by itself is not a recommended.  

If you want to use the Global object you can do so, but then you need 
to make use of Java's ThreadLocal variables to avoid threading 
conflict and related issues.

== Ezra E.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
With HiveMind integration (or even before then if we wanted to make 
such a change), it would be nice to allow Global and Visit, etc, to be 
configurable from .application.  In the meantime, to pull .application 
properties into Global, I do this in a custom BaseEngine extension:

   protected Object createGlobal(RequestContext context) {
     String indexPath = 
getPropertySource().getPropertyValue("index.path");

     Global global = new Global();
     global.setIndexPath(indexPath);
     return global;
   }


On Feb 14, 2004, at 7:30 PM, David Moran wrote:
> Couldn't you just create your own properties file in your custom global
> object?


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by David Moran <dm...@nc.rr.com>.
Couldn't you just create your own properties file in your custom global
object?


On Sat, 2004-02-14 at 11:35, Harish Krishnaswamy wrote:
> You could extend BaseEngine and override createGlobal() and initialize 
> your Global like you wish. The other option that would be appropriate 
> for this kind of initialization is application extensions.
> 
> -Harish
> 
> Petter Måhlén wrote:
> 
> >>am not clear how
> >>some.application:
> >><application name="someName" 
> >>engine-class="org.dcg.platform.StdTapestryEngine">
> >>   <description>desc</description>
> >><property name="org.apache.tapestry.visit-class" 
> >>value="org.dcg.platform.Visit" />
> >><property name="org.apache.tapestry.global-class" 
> >>value="org.dcg.platform.Global" />
> >>
> >>You just use it at the point where you need it first.
> >>I just defined it with a bunch of static methods in there so I can do 
> >>Global.someMethod().
> >>It looks like i'm not even doing new Global() anywhere. And 
> >>if i did it 
> >>would just be at the first (and last) time i needed the Global.
> >>    
> >>
> >
> >I guess my question isn't very clear; I've come that far already, so no
> >problem there. What I now want to do is in the application specification:
> >
> > <property name="se.elevance.my-property" value ="my property has this
> >value" />
> >
> >and in the Global class something like:
> >
> > myString = getPropertyValue("se.elevance.my-property");
> >
> >But I can't figure that last step out. Maybe I'm trying to do something that
> >goes against the Tapestry grain. I suppose I could provide some Page class
> >or something that always does something like:
> >
> > global.initialise(engine.getPropertySource());
> >
> >and have all my pages inherit from that class, but that's what I would like
> >to avoid. It's just not very elegant in my opinion, the Global class is not
> >really tied to a user session or a page view in itself.
> >
> >/ Petter
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
> >  
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by Harish Krishnaswamy <hk...@comcast.net>.
You could extend BaseEngine and override createGlobal() and initialize 
your Global like you wish. The other option that would be appropriate 
for this kind of initialization is application extensions.

-Harish

Petter Måhlén wrote:

>>am not clear how
>>some.application:
>><application name="someName" 
>>engine-class="org.dcg.platform.StdTapestryEngine">
>>   <description>desc</description>
>><property name="org.apache.tapestry.visit-class" 
>>value="org.dcg.platform.Visit" />
>><property name="org.apache.tapestry.global-class" 
>>value="org.dcg.platform.Global" />
>>
>>You just use it at the point where you need it first.
>>I just defined it with a bunch of static methods in there so I can do 
>>Global.someMethod().
>>It looks like i'm not even doing new Global() anywhere. And 
>>if i did it 
>>would just be at the first (and last) time i needed the Global.
>>    
>>
>
>I guess my question isn't very clear; I've come that far already, so no
>problem there. What I now want to do is in the application specification:
>
> <property name="se.elevance.my-property" value ="my property has this
>value" />
>
>and in the Global class something like:
>
> myString = getPropertyValue("se.elevance.my-property");
>
>But I can't figure that last step out. Maybe I'm trying to do something that
>goes against the Tapestry grain. I suppose I could provide some Page class
>or something that always does something like:
>
> global.initialise(engine.getPropertySource());
>
>and have all my pages inherit from that class, but that's what I would like
>to avoid. It's just not very elegant in my opinion, the Global class is not
>really tied to a user session or a page view in itself.
>
>/ Petter
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: When and where to do application initialisation?

Posted by Petter Måhlén <pe...@chello.se>.
> am not clear how
> some.application:
> <application name="someName" 
> engine-class="org.dcg.platform.StdTapestryEngine">
>    <description>desc</description>
> <property name="org.apache.tapestry.visit-class" 
> value="org.dcg.platform.Visit" />
> <property name="org.apache.tapestry.global-class" 
> value="org.dcg.platform.Global" />
> 
> You just use it at the point where you need it first.
> I just defined it with a bunch of static methods in there so I can do 
> Global.someMethod().
> It looks like i'm not even doing new Global() anywhere. And 
> if i did it 
> would just be at the first (and last) time i needed the Global.

I guess my question isn't very clear; I've come that far already, so no
problem there. What I now want to do is in the application specification:

 <property name="se.elevance.my-property" value ="my property has this
value" />

and in the Global class something like:

 myString = getPropertyValue("se.elevance.my-property");

But I can't figure that last step out. Maybe I'm trying to do something that
goes against the Tapestry grain. I suppose I could provide some Page class
or something that always does something like:

 global.initialise(engine.getPropertySource());

and have all my pages inherit from that class, but that's what I would like
to avoid. It's just not very elegant in my opinion, the Global class is not
really tied to a user session or a page view in itself.

/ Petter


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by "F. Da Costa" <da...@xs4all.nl>.
Petter Måhlén wrote:

>>>properly. My question is where I would best do that?
>>
>>Just create a Global object. It can be used by any class 
>>within Tapestry 
>>(so its not visit depended).
>>Just create it when you start up the app or do lazy instantiation.
> 
> 
> [snip]
>  
> Thanks, but I still don't quite get it. I can specify the class of the
> Global object through the application specification, and there's quite a few
> ways that I can specify parameters to the application according to the
> developer's guide. The one I would like to use, I think, is in the
> application specification, using a <property /> tag. But I am not clear how
some.application:
<application name="someName" engine-class="org.dcg.platform.StdTapestryEngine">
   <description>desc</description>
<property name="org.apache.tapestry.visit-class" 
value="org.dcg.platform.Visit" />
<property name="org.apache.tapestry.global-class" 
value="org.dcg.platform.Global" />

You just use it at the point where you need it first.
I just defined it with a bunch of static methods in there so I can do 
Global.someMethod().
It looks like i'm not even doing new Global() anywhere. And if i did it 
would just be at the first (and last) time i needed the Global.

> I can get my Global class to access that. Apparently, there is something I
> can use in IEngine that will get me to IPropertySource. But as I understand
> it, engines is only available on a per-session basis? Is there a global
> IPropertySource, or can I somehow access a valid engine during application
> start-up? Or do I have to make a call to some initialisation method in the
> Global object when requests are made? (I'd like to avoid that if I can)
> 
> / Petter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: When and where to do application initialisation?

Posted by Petter Måhlén <pe...@chello.se>.
> > properly. My question is where I would best do that?
> Just create a Global object. It can be used by any class 
> within Tapestry 
> (so its not visit depended).
> Just create it when you start up the app or do lazy instantiation.

[snip]
 
Thanks, but I still don't quite get it. I can specify the class of the
Global object through the application specification, and there's quite a few
ways that I can specify parameters to the application according to the
developer's guide. The one I would like to use, I think, is in the
application specification, using a <property /> tag. But I am not clear how
I can get my Global class to access that. Apparently, there is something I
can use in IEngine that will get me to IPropertySource. But as I understand
it, engines is only available on a per-session basis? Is there a global
IPropertySource, or can I somehow access a valid engine during application
start-up? Or do I have to make a call to some initialisation method in the
Global object when requests are made? (I'd like to avoid that if I can)

/ Petter


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by "F. Da Costa" <da...@xs4all.nl>.
Petter Måhlén wrote:

> Hi,
> 
> I'm trying to learn how to use Tapestry, and am putting together a very
> basic application. I am right now trying to get a page that reads some data
> from a database table to work, and to do that I of course need to ensure
> that the database connections and stuff are set up properly. My question is
> where I would best do that?
Just create a Global object. It can be used by any class within Tapestry 
(so its not visit depended).
Just create it when you start up the app or do lazy instantiation.

Fermin DCG


I would prefer not having some kind of check if
> the application is initialised every time a page is rendered. I'm looking
> for something like this, I think:
> 
> 1. servlet engine starts up
> 2. first request comes in
> 3. tapestry starts up
> 4. my application starts up - including configuration and startup of the
> back-end
> 5. page is shown
> 
> My questions are:
> 1) where best to put initial configuration data (like database URL and links
> to configuration files for the back-end, etc) - in the .application file
> maybe? 
> 2) where to place a call to the initialisation method of the back-end?
> 
> Thanks,
> 
> / Petter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by pr...@prajnait.com.
Of course to answer your question #1: I use the servlet container.  
Tomcat has a read-only JNDI service.  Declare the javax.sql.DataSource 
there and do a JNDI lookup to get it.  All completely standard -- this 
is realted to the servlet container and has nothing to do with 
Tapestry.

== Ezra E.

Quoting Petter Måhlén <pe...@chello.se>:

> Hi,
> 
> I'm trying to learn how to use Tapestry, and am putting together a
> very
> basic application. I am right now trying to get a page that reads
> some data
> from a database table to work, and to do that I of course need to
> ensure
> that the database connections and stuff are set up properly. My
> question is
> where I would best do that? I would prefer not having some kind of
> check if
> the application is initialised every time a page is rendered. I'm
> looking
> for something like this, I think:
> 
> 1. servlet engine starts up
> 2. first request comes in
> 3. tapestry starts up
> 4. my application starts up - including configuration and startup of
> the
> back-end
> 5. page is shown
> 
> My questions are:
> 1) where best to put initial configuration data (like database URL
> and links
> to configuration files for the back-end, etc) - in the .application
> file
> maybe? 
> 2) where to place a call to the initialisation method of the
> back-end?
> 
> Thanks,
> 
> / Petter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tapestry-user-help@jakarta.apache.org
> 
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: When and where to do application initialisation?

Posted by Jonny Wray <jo...@yahoo.com>.
Personally, I use the Spring framework to manage my service layer. It allows
you to abstract out all your database stuff to that layer, and takes care of
a lot of plumbing code (transactions, sql exception checking, etc). You code
becomes a lot shorter and cleaner. It has hooks into raw sql and some
persistance engines (eg Hibernate).

To initialise, you specify a servlet within web.xml that does the
initialisation of spring, and after that you can refer to the services you
define anywhere. It basically takes care of the concerns you are talking
about, plus other thread and database session issues with a multi-user
application.

There's a document on the spring site (http://www.springframework.com) about
integrating with tapestry.

Jonny


On 2/14/04 4:46 AM, "Petter Måhlén" <pe...@chello.se> wrote:

> Hi,
> 
> I'm trying to learn how to use Tapestry, and am putting together a very
> basic application. I am right now trying to get a page that reads some data
> from a database table to work, and to do that I of course need to ensure
> that the database connections and stuff are set up properly. My question is
> where I would best do that? I would prefer not having some kind of check if
> the application is initialised every time a page is rendered. I'm looking
> for something like this, I think:
> 
> 1. servlet engine starts up
> 2. first request comes in
> 3. tapestry starts up
> 4. my application starts up - including configuration and startup of the
> back-end
> 5. page is shown
> 
> My questions are:
> 1) where best to put initial configuration data (like database URL and links
> to configuration files for the back-end, etc) - in the .application file
> maybe? 
> 2) where to place a call to the initialisation method of the back-end?
> 
> Thanks,
> 
> / Petter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org