server side entry point to open/close database?

112 views
Skip to first unread message

Magnus

unread,
Jun 20, 2010, 5:23:16 AM6/20/10
to Google Web Toolkit
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

roji

unread,
Jun 20, 2010, 9:58:50 AM6/20/10
to Google Web Toolkit
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

Magnus

unread,
Jun 23, 2010, 9:51:40 AM6/23/10
to Google Web Toolkit
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
> > Magnus- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

roji

unread,
Jun 24, 2010, 1:54:31 AM6/24/10
to Google Web Toolkit
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
Reply all
Reply to author
Forward
0 new messages