From: Marco Zanon Date: Fri, 23 Oct 2015 14:00:59 +0000 (+0000) Subject: Implemented MSqlConnectionPool for database connection pooling. X-Git-Tag: 4.0~9 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=80b9d36ced16aed3b1e1999ea85873c2733ec825;p=Macaco Implemented MSqlConnectionPool for database connection pooling. --- diff --git a/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java b/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java new file mode 100644 index 0000000..940eaff --- /dev/null +++ b/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java @@ -0,0 +1,89 @@ +/** + * Macaco + * Copyright (c) 2009-2015 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.sql; + +import com.marcozanon.macaco.MObject; +import java.util.LinkedList; + +public class MSqlConnectionPool extends MObject { + + protected MSqlConnectionGenerator connectionGenerator = null; + + protected int minimumSize = 0; + protected int maximumSize = 0; + protected LinkedList connectionPool = new LinkedList(); + + /* */ + + public MSqlConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize) throws MSqlConnectionFailureException { + super(); + // + if (0 > minimumSize) { + throw new IllegalArgumentException(String.format("Invalid 'minimumSize': %s.", minimumSize)); + } + if (1 > maximumSize) { + throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s.", maximumSize)); + } + else if (minimumSize > maximumSize) { + throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s < %s ('minimumSize').", maximumSize, minimumSize)); + } + // + this.connectionGenerator = new MSqlConnectionGenerator(driver, url, username, password); + this.minimumSize = minimumSize; + this.maximumSize = maximumSize; + // + this.initializeConnectionPool(); + } + + /* Connection generator */ + + protected MSqlConnectionGenerator getConnectionGenerator() { + return this.connectionGenerator; + } + + /* Connection pool */ + + protected int getMinimumSize() { + return this.minimumSize; + } + + protected int getMaximumSize() { + return this.maximumSize; + } + + protected LinkedList getConnectionPool() { + return this.connectionPool; + } + + protected void initializeConnectionPool() throws MSqlConnectionFailureException { + for (int c = 0; c < this.getMinimumSize(); c++) { + MSqlConnection connection = this.getConnectionGenerator().getConnection(); + this.getConnectionPool().add(connection); + } + } + + public synchronized MSqlConnection popConnection() throws MSqlConnectionFailureException { + LinkedList connectionPool = this.getConnectionPool(); + MSqlConnection connection = connectionPool.removeLast(); + if (this.getMinimumSize() > connectionPool.size()) { + connectionPool.add(this.getConnectionGenerator().getConnection()); + } + return connection; + } + + public synchronized void pushConnection(MSqlConnection connection) { + if (null == connection) { + throw new IllegalArgumentException("Invalid 'connection': null."); + } + // + LinkedList connectionPool = this.getConnectionPool(); + if (this.getMaximumSize() >= connectionPool.size()) { + connectionPool.add(connection); + } + } + +}