The documentation of the SQLServerXADataSource interface, says that it provides connection pooling and whenever a client is done with the connection, should call the close() method; in this case the connection won't be closed but returned to the pool. I'm trying this, but next time I'm trying to get a connection it says the connection is already closed. How can I implement the connection pooling using this interface?
The class that provides the connection is:
public class ConnectionPool {
SQLServerXADataSource ds;
Decoder dec = Base64.getDecoder();
Connection connection = null;
public ConnectionPool(InputStream inputStream) throws Exception {
ds = new SQLServerXADataSource();
boolean res = Config.load(inputStream);
if (res) {
ds.setServerName(Config.SQL_SERVER());
ds.setDatabaseName(Config.SQL_DB_NAME());
ds.setIntegratedSecurity(Config.SQL_INTEGRATED_SECURITY());
ds.setPortNumber(Config.SQL_PORT());
if (!Config.SQL_INTEGRATED_SECURITY()) {
ds.setUser(Config.SQL_USERNAME());
ds.setPassword(new String(dec.decode(Config.SQL_USER_PASSWORD())));
}
} else {
throw new Exception("Failed to load parameters.");
}
connection = ds.getConnection();
}
public Connection getConnection() throws SQLServerException {
return this.connection;
}
}
After creating the object of ConnectionPool (when the user is logged in), I save it as a session attribute so that the servlets can get it when they need it.
Can you please provide an example of taking advantage of this interface to implement connection pooling in a web application? I know I'm not doing it right, but cannot figure out where the problem is.