panda.dao.sql.dbcp.ThreadLocalDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.dao.sql.dbcp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ThreadLocalDataSource extends AbstractDataSource {
private ThreadLocal local = new ThreadLocal();
/**
* Constructor
*/
public ThreadLocalDataSource() {
super();
}
@Override
protected Connection popConnection() throws SQLException {
ThreadLocalConnection c = local.get();
if (c == null || c.isClosed()) {
c = newConnection();
local.set(c);
}
return c;
}
protected ThreadLocalConnection newConnection() throws SQLException {
ThreadLocalConnection c = new ThreadLocalConnection(DriverManager.getConnection(jdbc.url, prop));
Connection r = c.getRealConnection();
if (r.getAutoCommit() != jdbc.autoCommit) {
r.setAutoCommit(jdbc.autoCommit);
}
return c;
}
}