Hi Magnus.

The standard "enterprisey" way to do this, would be to use a
javax.sql.DataSource as a representation of your database. Your
RemoteServiceServlet would look up this object in JNDI at
initialization time, and create connections by calling
"getConnection()" on it each time a user connect.

The actual connection pool would be managed outside your application.
For example, when you define a DataSource resource in Tomcat, an
Apache connection pool (DBCP) is created. This way, when your
application asks for a new connection, it will be transparently
reusing pooled connections.

Example servlet:
class MyServlet
   extends RemoteServiceServlet
   implements MyServletService
{
   @Resource(name="jdbc/db")   // This makes your servlet container
inject the JNDI resource found under jdbc/db
   private DataSource db;

   public void myGwtMethod() {
      Connection conn = db.getConnection();
   }
   ...
}

If you're using Tomcat, take a look here for how to manage DataSource
resources: 
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html.

To answer your specific question, if you want code to run once at init-
time, override init(). If you want code to run once at shutdown time,
override destroy(). But you shouldn't need these, since the @Resource
annotation will cause the container to do the initialization for you.

... and don't forget to close() your connections, even if exceptions
were raised, otherwise you'll have a leak.

Shay

On Jun 23, 9:51 am, Magnus <[email protected]> wrote:
> Hello,
>
> after your post I would open/close my db within each service call.
>
> If I would do it with a pool, I would need 2 entry points, one to open
> the pool and one to close it. If I would go this way, would these
> entry point exist (is there a deinit)?
>
> Thanks
> Magnus
>
> On 20 Jun., 15:58, roji <[email protected]> wrote:
>
>
>
> > Hi Magnus.
>
> > Your server-side service implementation is a class that extends GWT's
> > RemoteServiceServlet, which itself extends the Servlet interface. If
> > you need to perform once-only initialization, you can do so by
> > overriding the init() method in your class.
>
> > Note, however, that init() is probably a bad place to open database
> > connections, as your backend will be accessed concurrently by many
> > users and multiple database connections are needed. You should
> > probably either open and close database connections inside the RPC
> > service calls themselves (using a database connection pool behind the
> > scenes), or in some scenarios store some resources in the user's HTTP
> > session (acquired by calling
> > this.getThreadLocalRequest().getSession()).
>
> > If you need more help, can you send more details about your
> > application?
>
> > Shay
>
> > On Jun 20, 5:23 am, Magnus <[email protected]> wrote:
>
> > > Hi,
>
> > > I wonder where to open and close the database on the server side, and
> > > where to store the database connection.
>
> > > At this point, I only have the server side implementation of my RPC
> > > services. But there is no place to store my Connection object and no
> > > entry point like "onServerLoad" or something like that.
>
> > > An alternative way would be to open and close the database on each
> > > service call.
>
> > > How do you do this?
>
> > > Thank you
> > > Magnus- Zitierten Text ausblenden -
>
> > - Zitierten Text anzeigen -

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to