Skip to content

Commit

Permalink
Recovery for 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanovicz committed Jan 20, 2023
1 parent 007d60d commit 7984f21
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ private void writeObject(ObjectOutputStream out) throws IOException
{
if (model == null)
{
// the only way this can happen is when instance has ben deserialized
// the only way this can happen is when instance has been deserialized
// while corresponding model has never been initialized
throw new IOException("cannot serialize instance again: its model was never initialized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.sql.DataSource;

/**
Expand All @@ -38,42 +39,40 @@
*/
public class ConnectionPool implements Serializable
{
private static final int VALIDATION_TIMEOUT = 1; // 1s is long enough

protected static Logger logger = LoggerFactory.getLogger("sql");
private static Random randomizer = new Random();

/**
* Constructor.
* @param schema schema
* @param driverInfos infos on the driverInfos
* @param autocommit autocommit
* @param max max connections
* @throws SQLException
* /
public ConnectionPool(String schema, DriverInfos driverInfos, boolean autocommit, int max)
throws SQLException
{
this.schema = schema;
this.driverInfos = driverInfos;
this.autocommit = autocommit;
connections = new ArrayList<ConnectionWrapper>();
this.max = max;
}
*/

/**
*
* @param dataSource
* @param schema
* @param max
* @throws SQLException
*/
public ConnectionPool(DataSource dataSource, Credentials credentials, DriverInfos driverInfos, String schema, boolean autocommit, int max) throws SQLException
public ConnectionPool(DataSource dataSource, Credentials credentials, DriverInfos driverInfos, String schema, boolean autocommit, int max, boolean checkConnections) throws SQLException
{
this.dataSource = dataSource;
this.credentials = credentials;
this.driverInfos = driverInfos;
this.schema = schema;
this.autocommit = autocommit;
this.max = max;
this.checkConnections = checkConnections;
}

public ConnectionPool(DataSource dataSource, Credentials credentials, DriverInfos driverInfos, String schema, boolean autocommit, int max) throws SQLException
{
this(dataSource, credentials, driverInfos, schema, autocommit, max, true);
}

public ConnectionPool(DataSource dataSource, Credentials credentials, DriverInfos driverInfos, String schema, boolean autocommit) throws SQLException
{
this(dataSource, credentials, driverInfos, schema, autocommit, 50, true);
}

public ConnectionPool(DataSource dataSource, Credentials credentials, DriverInfos driverInfos, String schema) throws SQLException
{
this(dataSource, credentials, driverInfos, schema, true, 50, true);
}

public DataSource getDataSource()
Expand All @@ -96,22 +95,24 @@ public synchronized ConnectionWrapper getConnection() throws SQLException
for(Iterator it = connections.iterator(); it.hasNext(); )
{
ConnectionWrapper c = (ConnectionWrapper)it.next();

if(c.isClosed())
if (!c.isBusy())
{
it.remove();
}
else if(!c.isBusy())
{
return c;
if(c.isClosed() || checkConnections && !c.isValid(VALIDATION_TIMEOUT))
{
it.remove();
}
else
{
return c;
}
}
}
if(connections.size() == max)
{
logger.warn("Connection pool: max number of connections reached! ");

// return a busy connection...
return connections.get(0);
return connections.get(randomizer.nextInt(connections.size()));
}

ConnectionWrapper newconn = createConnection();
Expand Down Expand Up @@ -199,4 +200,7 @@ public void clear()

/** Maximum number of connections. */
private int max;

/** whether to check connections */
private boolean checkConnections;
}

0 comments on commit 7984f21

Please sign in to comment.