/*@VaadinApache2LicenseForJavaFiles@ */packagecom.vaadin.data.util.sqlcontainer.connection;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.HashSet;importjava.util.Set;/** * Simple implementation of the JDBCConnectionPool interface. Handles loading * the JDBC driver, setting up the connections and ensuring they are still * usable upon release. */@SuppressWarnings("serial")publicclassSimpleJDBCConnectionPoolimplementsJDBCConnectionPool{privateintinitialConnections=5;privateintmaxConnections=20;privateStringdriverName;privateStringconnectionUri;privateStringuserName;privateStringpassword;privatetransientSet<Connection>availableConnections;privatetransientSet<Connection>reservedConnections;privatebooleaninitialized;publicSimpleJDBCConnectionPool(StringdriverName,StringconnectionUri,StringuserName,Stringpassword)throwsSQLException{if(driverName==null){thrownewIllegalArgumentException("JDBC driver class name must be given.");}if(connectionUri==null){thrownewIllegalArgumentException("Database connection URI must be given.");}if(userName==null){thrownewIllegalArgumentException("Database username must be given.");}if(password==null){thrownewIllegalArgumentException("Database password must be given.");}this.driverName=driverName;this.connectionUri=connectionUri;this.userName=userName;this.password=password;/* Initialize JDBC driver */try{Class.forName(driverName).newInstance();}catch(Exceptionex){thrownewRuntimeException("Specified JDBC Driver: "+driverName+" - initialization failed.",ex);}}publicSimpleJDBCConnectionPool(StringdriverName,StringconnectionUri,StringuserName,Stringpassword,intinitialConnections,intmaxConnections)throwsSQLException{this(driverName,connectionUri,userName,password);this.initialConnections=initialConnections;this.maxConnections=maxConnections;}privatevoidinitializeConnections()throwsSQLException{availableConnections=newHashSet<Connection>(initialConnections);reservedConnections=newHashSet<Connection>(initialConnections);for(inti=0;i<initialConnections;i++){availableConnections.add(createConnection());}initialized=true;}publicsynchronizedConnectionreserveConnection()throwsSQLException{if(!initialized){initializeConnections();}if(availableConnections.isEmpty()){if(reservedConnections.size()<maxConnections){availableConnections.add(createConnection());}else{thrownewSQLException("Connection limit has been reached.");}}Connectionc=availableConnections.iterator().next();availableConnections.remove(c);reservedConnections.add(c);returnc;}publicsynchronizedvoidreleaseConnection(Connectionconn){if(conn==null||!initialized){return;}/* Try to roll back if necessary */try{if(!conn.getAutoCommit()){conn.rollback();}}catch(SQLExceptione){/* Roll back failed, close and discard connection */try{conn.close();}catch(SQLExceptione1){/* Nothing needs to be done */}reservedConnections.remove(conn);return;}reservedConnections.remove(conn);availableConnections.add(conn);}privateConnectioncreateConnection()throwsSQLException{Connectionc=DriverManager.getConnection(connectionUri,userName,password);c.setAutoCommit(false);if(driverName.toLowerCase().contains("mysql")){try{Statements=c.createStatement();s.execute("SET SESSION sql_mode = 'ANSI'");s.close();}catch(Exceptione){// Failed to set ansi mode; continue}}returnc;}publicvoiddestroy(){for(Connectionc:availableConnections){try{c.close();}catch(SQLExceptione){// No need to do anything}}for(Connectionc:reservedConnections){try{c.close();}catch(SQLExceptione){// No need to do anything}}}privatevoidwriteObject(java.io.ObjectOutputStreamout)throwsIOException{initialized=false;out.defaultWriteObject();}}