001package org.javasimon.jdbcx4; 002 003import java.sql.Connection; 004import java.sql.SQLException; 005import java.util.HashMap; 006import java.util.Map; 007import javax.sql.*; 008 009import org.javasimon.jdbc4.SimonConnection; 010 011/** 012 * Simon implementation of <code>PooledConnection</code>, needed for 013 * Simon ConnectionPollDataSource implementation. 014 * <p/> 015 * All method invokes its real implementation. 016 * <p/> 017 * See the {@link org.javasimon.jdbcx4 package description} for more 018 * information. 019 * 020 * @author Radovan Sninsky 021 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a> 022 * @since 2.4 023 */ 024public class SimonPooledConnection implements PooledConnection { 025 026 private class SimonConnectionEventListener implements ConnectionEventListener { 027 028 private final ConnectionEventListener originalListener; 029 030 SimonConnectionEventListener(ConnectionEventListener originalListener) { 031 this.originalListener = originalListener; 032 } 033 034 @Override 035 public void connectionClosed(ConnectionEvent event) { 036 originalListener.connectionClosed(new ConnectionEvent(SimonPooledConnection.this, event.getSQLException())); 037 } 038 039 @Override 040 public void connectionErrorOccurred(ConnectionEvent event) { 041 originalListener.connectionErrorOccurred(new ConnectionEvent(SimonPooledConnection.this, event.getSQLException())); 042 } 043 } 044 045 private class SimonStatementEventListener implements StatementEventListener { 046 047 private final StatementEventListener originalListener; 048 049 SimonStatementEventListener(StatementEventListener originalListener) { 050 this.originalListener = originalListener; 051 } 052 053 @Override 054 public void statementClosed(StatementEvent event) { 055 originalListener.statementClosed(new StatementEvent(SimonPooledConnection.this, event.getStatement())); 056 } 057 058 @Override 059 public void statementErrorOccurred(StatementEvent event) { 060 originalListener.statementErrorOccurred(new StatementEvent(SimonPooledConnection.this, event.getStatement(), event.getSQLException())); 061 } 062 } 063 064 private final PooledConnection pooledConn; 065 private final String prefix; 066 private Map<ConnectionEventListener, SimonConnectionEventListener> connListeners = new HashMap<>(); 067 private Map<StatementEventListener, SimonStatementEventListener> stmtListeners = new HashMap<>(); 068 069 /** 070 * Class constructor. 071 * 072 * @param connection real pooled connection 073 * @param prefix Simon prefix 074 */ 075 public SimonPooledConnection(PooledConnection connection, String prefix) { 076 this.pooledConn = connection; 077 this.prefix = prefix; 078 } 079 080 @Override 081 public final Connection getConnection() throws SQLException { 082 return new SimonConnection(pooledConn.getConnection(), prefix); 083 } 084 085 @Override 086 public final void close() throws SQLException { 087 pooledConn.close(); 088 } 089 090 @Override 091 public final void addConnectionEventListener(ConnectionEventListener listener) { 092 connListeners.put(listener, new SimonConnectionEventListener(listener)); 093 pooledConn.addConnectionEventListener(connListeners.get(listener)); 094 } 095 096 @Override 097 public final void removeConnectionEventListener(ConnectionEventListener listener) { 098 pooledConn.removeConnectionEventListener(connListeners.get(listener)); 099 connListeners.remove(listener); 100 } 101 102 @Override 103 public void addStatementEventListener(StatementEventListener listener) { 104 stmtListeners.put(listener, new SimonStatementEventListener(listener)); 105 pooledConn.addStatementEventListener(stmtListeners.get(listener)); 106 } 107 108 @Override 109 public void removeStatementEventListener(StatementEventListener listener) { 110 pooledConn.removeStatementEventListener(stmtListeners.get(listener)); 111 stmtListeners.remove(listener); 112 } 113}