package de.thinkbaer.aios.jdbc.query;


import java.sql.Statement;

import de.thinkbaer.aios.api.datasource.query.QueryResults;
import de.thinkbaer.aios.jdbc.ConnectionImpl;

public abstract class AbstractQueryImpl<X extends QueryResults, Y extends AbstractQueryImpl> {

	private String sql;
	
	
	public abstract X execute(ConnectionImpl conn)  throws Exception;
	
	public Statement tryAcquireStatement(ConnectionImpl connection) throws Exception{
		return tryAcquireStatement(connection, 5, 500);		
	}
	
	public Statement tryAcquireStatement(ConnectionImpl connection, int retry, int sleep) throws Exception{
		Statement stat = null;
		try{
			stat = connection.getConnection().createStatement();
		}catch(Exception e){
			if(retry > 0){
				connection.close();
				Thread.sleep(sleep);
				return tryAcquireStatement(connection, --retry, sleep);
			}else{
				throw e;
			}
		}
		return stat;
		
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}
	
	public Y sql(String query){
		setSql(query);
		return (Y) this;
	}

}

@@ -287,16 +287,96 @@
  sql;%0A%09%0A
+%09private static final Logger L = LogManager.getLogger(AbstractQueryImpl.class);%0A
 %09%0A%09publi
@@ -812,16 +812,103 @@
 y %3E 0)%7B%0A
+%09%09%09    L.warn(%22tryAcquireStatement problem: %22 + e.getMessage() + %22 Retry: %22 + retry);%0A%0A
 %09%09%09%09conn

add log on try acquire statement