Federico
Federico

Reputation: 433

In a multi thread scenario is it safe to use a static instance of the Tomcat JDBC DataSource object?

I am using the Tomcat JDBC connection pool.

In a multi thread scenario, is it safe to use a static instance of the DataSource object?

This is how I am getting my connection when needed:

 public static Connection getConnection() throws SQLException {
        return datasource.getConnection();
      }

After I run my querie(s) I make sure the close the connection.

Upvotes: 0

Views: 339

Answers (1)

Jilles van Gurp
Jilles van Gurp

Reputation: 8294

Generally, yes. While the DataSource interface does not specify this explicitly, typically you'd use some connection pooling DataSource implementation in an application server, which is typically designed to work well in multi threaded environments. On paper some implementations might actually be mutable but typically that should not be the case.

Upvotes: 1

Related Questions