Discussions

General J2EE: Accessing JDBC DataSource in a multi-thread environment

  1. I read a lot of discussions about it but I am still confused: is the getConnection method of a DataSource class thread safe? I'd like to avoid multiple JNDI lookup sharing the datasource instance, wrapped in a singleton, among all j2ee threads. Thanks in advance. emi.
  2. Correct me if I am wrong. It depends on your connection pool implementations but most likely all connection pool implementations should be thread-safe so you do not have to worry about that. One example: you can get connection from datasource on different JSPs.
  3. I am struggling to think about any instance fields or other kind of thread-unsafe stuff the one can use in simplistic DataSource implementation (it all depends on the implementation and back-end DB though). If it comes to DataSources with added value in form of built-in pooling support then they must take care about thread-safety. Hint: never get connections in JSPs.