You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Asaf Paris Mandoki <as...@gmail.com> on 2007/05/14 05:01:53 UTC

Persistent Database Object

Hello,
I understand, form reading the Walking Tour of the Struts 2 MailReader
Application that they use an object that is created when the
application starts, and stays there forever, to manage the data source
(XML).


I'm planning to make an application that uses a MySQL Database.

Should I have a persistent object with a persistent connection to the
database or is it better to create the connection each time i use it?

Thanks,
Asaf

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


Re: Persistent Database Object

Posted by Josh Long <st...@gmail.com>.
Use a Connection pool.

Jakarta Commons has one that's comonly available and easily configured in
Tomcat, Spring, in code, etc.

Make the Connection pool persistant (As it might be if it were available in
a Spring context, for example.), and then on each new "request" (ie, if
you're going to use it for a method block, or something) grab a connection
and be sure to follow the heuristics for resource release and close the
connection.

-- 
Joshua Long
Sun Certified Java Programmer
http://www.joshlong.com/

On 5/13/07, Asaf Paris Mandoki <as...@gmail.com> wrote:
>
> Hello,
> I understand, form reading the Walking Tour of the Struts 2 MailReader
> Application that they use an object that is created when the
> application starts, and stays there forever, to manage the data source
> (XML).
>
>
> I'm planning to make an application that uses a MySQL Database.
>
> Should I have a persistent object with a persistent connection to the
> database or is it better to create the connection each time i use it?
>
> Thanks,
> Asaf
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Persistent Database Object

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Asaf,

Asaf Paris Mandoki wrote:
> I'm planning to make an application that uses a MySQL Database.
> 
> Should I have a persistent object with a persistent connection to the
> database or is it better to create the connection each time i use it?

It really depends on what your application needs to do. For instance, if
your application needs access to the database only sometimes, you might
want to open the connection only when you need it. On the other hand, if
you need information from the database all the time, you should
definitely use a connection pool (see another response).

If you need to login to the database with the credentials of individual
users, then you don't really have a choice: you'll need to open a
different connection for each user (unless your JDBC driver offers a way
to switch users after a generic connection is made or something like that).

Most webapps use connection pools because they need constant access to
the database and are able to use an "application" user to connect.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGSGC59CaO5/Lv0PARArGQAJ0VpI0hkjfbON5ISKR6abNpOywKeQCaA/j7
dZQvCyYEt4b5qUs51M+Iucw=
=GPG0
-----END PGP SIGNATURE-----

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