package de.ausb.dbconnectionmanager;
..................
import de.ausb.dbconnectionmanager.threads.WaitingThread;
public class ConnectionDOM {
private String host;
private String scheme;
private String user;
private String password;
private boolean persistent;
private int connectionPoolSize;
//Speicherung der dauerhaften Datenbankverbindungen
private ArrayList<ConnectionDecorator> connections;
//wartende Threads (nur für dauerhafte Verbindung -> persistent==true)
private ArrayList<WaitingThread> waitingThreads;
public ConnectionDOM(String host, String scheme, String user,
String password, int connectionPoolSize, boolean persistent) {
setHost(host);
setScheme(scheme);
setUser(user);
setPassword(password);
setConnectionPoolSize(connectionPoolSize);
setPersistent(persistent);
}
public ConnectionDOM() {
setConnectionPoolSize(1);
setPersistent(false);
}
private ConnectionDecorator openConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + getHost() + "/" + getScheme();
Connection connectionLok = DriverManager.getConnection(url,
getUser(), getPassword());
if (this.isPersistent()) {
return new ConnectionDecorator
(connectionLok, isPersistent(),
this);
} else {
return new ConnectionDecorator(connectionLok);
}
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
public ConnectionDecorator getConnection() throws SQLException {
if (!isPersistent()) {
return openConnection();
} else {
if (getConnections().size() < getConnectionPoolSize()) {
ConnectionDecorator con = openConnection();
getConnections().add(con);
con.setReleased(false);
return con;
} else {
WaitingThread currThread = new WaitingThread();
getWaitingThreads().add(currThread);
while (true) {
synchronized (this) {
for (ConnectionDecorator c : getConnections()) {
if (c.isClosed()) {
ConnectionDecorator newCD = openConnection();
newCD.setReleased(false);
getConnections().add(newCD);
getConnections().remove(c);
getWaitingThreads().remove(currThread);
return newCD;
} else if (c.isReleased()) {
c.setReleased(false);
getWaitingThreads().remove(currThread);
return c;
}
}
}
try {
currThread.suspend();
} catch (InterruptedException e) {
return null;
}
}
}
}
}
private String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
private String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String getScheme() {
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
private String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public int getConnectionPoolSize() {
return connectionPoolSize;
}
public void setConnectionPoolSize(int connectionPool) {
this.connectionPoolSize = connectionPool;
}
private ArrayList<ConnectionDecorator> getConnections() {
return connections;
}
private void setConnections(ArrayList<ConnectionDecorator> connections) {
this.connections = connections;
}
public boolean isPersistent() {
return persistent;
}
public void setPersistent(boolean persistent) {
this.persistent = persistent;
if (persistent) {
setConnections(new ArrayList<ConnectionDecorator>());
setWaitingThreads(new ArrayList<WaitingThread>());
}
}
public ArrayList<WaitingThread> getWaitingThreads() {
return waitingThreads;
}
private void setWaitingThreads(ArrayList<WaitingThread> waitingThreads) {
this.waitingThreads = waitingThreads;
}
}