-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Saurabh,

On 4/4/14, 1:19 AM, Saurabh Saraswat wrote:
> I am doing connection pooling with tomcat 7.0.39 and MySQL
> 5.5.After searching on google and with your help i have done the
> below things. Even i am able to get the connection successfully
> using this but getting some trouble and exception. I am explaining
> you all steps done by me-
> 
> [snip]
> 
> *3. Then on my servlet i am getting the object of connection like
> this-*
> 
> private static InitialContext ic; protected static DataSource
> datasource; private static Context ctx;

You shouldn't use class members for these: instead, do everything
locally in your getConnection method. It will make your servlet less
prone to error. If you want to fetch the DataSource one single time,
do it in a ServletContextListener and place the DataSource into the
application scope.

Just don't fetch it with every request and store it into a static
class member: you may run into odd thread behavior if you do this.

> public void doSomeStuff() throws DatabaseException { Connection
> conn = null; try { conn= getConnection();
> 
> ..................................... // do the required stuff
> 
> } catch (Exception ex) {
> 
> } finally { conn.close(); } }

Don't forget to:

1. wrap a try/catch block around conn.close: you don't want an
SQLException thrown from conn.close to mask any exception that is in
the process of being thrown. In the method above, you are catching all
exceptions (but not Throwables like NoSuchMethodError, etc.) so you
could still mask things.

2. Close all other JDBC resources you use (Statement, ResultSet).

3. Catch all exceptions/errors if rollback is a possibility.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTPsqKAAoJEBzwKT+lPKRYNcQQAK3oNKSgXuCa1f3mFN/m/jPD
2YYkhmSFu7YpPdB6sZNvPPiPlce7Ddbxmqq9Rd84jhbxvn4C2cy0cNV7GAoJ7x8i
FnW+4UWGnnDKW3+9dS8D5JsdRbhplGYwdFWKI4smxXuZf+bUfvnpltQvtnVqacC8
cQu8tFy6XtlnxozlGKSurk4/6T+oqMSwneeuQxIh9bUvU3EwnX3aJGwcTvSNXJCk
vDOTQx+Z21Fv0CB0so33c/XfOKjB9r61zZs3GXahtpq3suCbi9Ch5hFv/FB9mjc9
cl4tGyzlbrV0kVpz4WSG9Q+/12bXt4W9aWamCH6ruZ1ddqeF9ONRmbb9dV1YZchK
Tf+/7WFB3o0Zn73/kYaDtnv1fOYBR0zDVIcO5zFNVvskLTMMzR55O2U+amujY/Aq
niPUPvBudf63H075DFJWj+9NeFUUduCgYUUgd+mmwj7PtN/0+Yu3RkUqUOEQbg0Y
OUqKrLw9G9EUYcNWlIrUk5U9PbN0pvNNpZ+jcKnYGQijSFkvWqX78YqZ5kP9yHVl
xJx0YwqL1UGZAjYUNZ276FVeA/UhVWInXzCRdjBmAvp8n6TrI39l0mftwtAh6AOQ
tyIPGio+Uvz9QrKl8azSRWQVIuwhEZcYluCn+M8zdc9yDq5u+bpNCoZxp7O50G9j
NMpO/UMsFes4OEOZTp+Q
=tV3f
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to