From eb78e0fc062038252dee70a333f72eeb5f0bdb77 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 17 Jul 2011 09:08:29 +0000 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81HA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/druid/trunk@461 b9813039-fb51-4c41-a8b9-e21c2acb5095 --- .../alibaba/druid/pool/ha/HAConnection.java | 42 ++- .../alibaba/druid/pool/ha/HAStatement.java | 274 +++++++++++++----- 2 files changed, 242 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/HAConnection.java b/src/main/java/com/alibaba/druid/pool/ha/HAConnection.java index 8daa7ffd6a..c9e33ea3e2 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HAConnection.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HAConnection.java @@ -45,21 +45,51 @@ public class HAConnection extends WrapperAdapter implements Connection, Connecti private Map attributes = null; - private Date connectedTime = new Date(); + private Date connectedTime = null; public HAConnection(HADataSource haDataSource, int id){ this.haDataSource = haDataSource; this.id = id; } - public HADataSource getHaDataSource() { - return haDataSource; - } - - private void checkConnection(String sql) throws SQLException { + public void checkConnection(String sql) throws SQLException { if (conn == null) { conn = haDataSource.getConnectionInternal(id); } + + if (autoCommit != null) { + conn.setAutoCommit(autoCommit); + } + + if (readOnly != null) { + conn.setReadOnly(readOnly); + } + + if (catalog != null) { + conn.setCatalog(catalog); + } + + if (transactionLeval != null) { + conn.setTransactionIsolation(transactionLeval); + } + + if (typeMap != null) { + conn.setTypeMap(typeMap); + } + + if (holdability != null) { + conn.setHoldability(holdability); + } + + if (clientInfo != null) { + conn.setClientInfo(clientInfo); + } + + connectedTime = new Date(); + } + + public HADataSource getHaDataSource() { + return haDataSource; } @Override diff --git a/src/main/java/com/alibaba/druid/pool/ha/HAStatement.java b/src/main/java/com/alibaba/druid/pool/ha/HAStatement.java index 668c0f8bc9..af90201c1d 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HAStatement.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HAStatement.java @@ -1,6 +1,5 @@ package com.alibaba.druid.pool.ha; -import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLWarning; @@ -15,6 +14,50 @@ public class HAStatement extends WrapperAdapter implements Statement { private Integer resultSetType; private Integer resultSetConcurrency; private Integer resultSetHoldability; + private Statement stmt; + + private Integer maxFieldSize; + private Integer maxRows; + private Boolean escapeProcessing; + private Integer queryTimeout; + private String cursorName; + private Integer fetchDirection; + private Integer fetchSize; + + void checkStatement(String sql) throws SQLException { + conn.checkConnection(sql); + + if (resultSetType != null && resultSetConcurrency != null && resultSetHoldability != null) { + stmt = conn.getConnectionRaw().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); + } else if (resultSetType != null && resultSetConcurrency != null) { + stmt = conn.getConnectionRaw().createStatement(resultSetType, resultSetConcurrency); + } else { + stmt = conn.getConnectionRaw().createStatement(); + } + + if (maxFieldSize != null) { + stmt.setMaxFieldSize(maxFieldSize); + } + if (maxRows != null) { + stmt.setMaxRows(maxRows); + } + if (escapeProcessing != null) { + stmt.setEscapeProcessing(escapeProcessing); + } + if (queryTimeout != null) { + stmt.setQueryTimeout(queryTimeout); + } + if (cursorName != null) { + stmt.setCursorName(cursorName); + } + if (fetchDirection != null) { + stmt.setFetchDirection(fetchDirection); + } + if (fetchSize != null) { + stmt.setFetchSize(fetchSize); + } + } + public HAStatement(HAConnection conn, long id){ super(); @@ -39,251 +82,346 @@ public class HAStatement extends WrapperAdapter implements Statement { this.resultSetHoldability = resultSetHoldability; } - public HAConnection getConn() { - return conn; - } - public long getId() { return id; } + @Override public ResultSet executeQuery(String sql) throws SQLException { - // TODO Auto-generated method stub - return null; + checkStatement(sql); + + return stmt.executeQuery(sql); } @Override public int executeUpdate(String sql) throws SQLException { - // TODO Auto-generated method stub - return 0; + checkStatement(sql); + return stmt.executeUpdate(sql); } @Override public void close() throws SQLException { - // TODO Auto-generated method stub - + if (stmt != null) { + stmt.close(); + } } @Override public int getMaxFieldSize() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getMaxFieldSize(); + } + + if (maxFieldSize == null) { + return 0; + } + + return maxFieldSize; } @Override public void setMaxFieldSize(int max) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setMaxFieldSize(max); + return; + } + this.maxFieldSize = max; } @Override public int getMaxRows() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getMaxRows(); + } + + if (maxRows == null) { + return 0; + } + + return maxRows; } @Override public void setMaxRows(int max) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setMaxRows(max); + return; + } + this.maxRows = max; } @Override public void setEscapeProcessing(boolean enable) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setEscapeProcessing(enable); + return; + } + this.escapeProcessing = enable; } @Override public int getQueryTimeout() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getQueryTimeout(); + } + + if (queryTimeout == null) { + return 0; + } + + return queryTimeout; } @Override public void setQueryTimeout(int seconds) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setQueryTimeout(seconds); + return; + } + this.queryTimeout = seconds; } @Override public void cancel() throws SQLException { - // TODO Auto-generated method stub - + if (stmt != null) { + stmt.cancel(); + } } @Override public SQLWarning getWarnings() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getWarnings(); + } + return null; } @Override public void clearWarnings() throws SQLException { - // TODO Auto-generated method stub - + if (stmt != null) { + stmt.clearWarnings(); + } } @Override public void setCursorName(String name) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setCursorName(name); + return; + } + this.cursorName = name; } @Override public boolean execute(String sql) throws SQLException { - // TODO Auto-generated method stub - return false; + checkStatement(sql); + return stmt.execute(sql); } @Override public ResultSet getResultSet() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getResultSet(); + } + return null; } @Override public int getUpdateCount() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getUpdateCount(); + } + return 0; } @Override public boolean getMoreResults() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getMoreResults(); + } + return false; } @Override public void setFetchDirection(int direction) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setFetchDirection(direction); + return; + } + this.fetchDirection = direction; } @Override public int getFetchDirection() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getFetchDirection(); + } + + return this.fetchDirection; } @Override public void setFetchSize(int rows) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + stmt.setFetchSize(rows); + return; + } + this.fetchSize = rows; } @Override public int getFetchSize() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getFetchSize(); + } + + return this.fetchSize; } @Override public int getResultSetConcurrency() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getResultSetConcurrency(); + } + + if (resultSetConcurrency != null) { + return resultSetConcurrency; + } + return 0; } @Override public int getResultSetType() throws SQLException { - // TODO Auto-generated method stub - return 0; + if (stmt != null) { + return stmt.getResultSetType(); + } + + return resultSetType; } @Override public void addBatch(String sql) throws SQLException { - // TODO Auto-generated method stub + checkStatement(sql); + stmt.addBatch(sql); } @Override public void clearBatch() throws SQLException { - // TODO Auto-generated method stub - + if (stmt != null) { + stmt.clearBatch(); + } } @Override public int[] executeBatch() throws SQLException { - // TODO Auto-generated method stub - return null; + if (stmt != null) { + return stmt.executeBatch(); + } + return new int[0]; } @Override - public Connection getConnection() throws SQLException { - // TODO Auto-generated method stub - return null; + public HAConnection getConnection() throws SQLException { + return conn; } @Override public boolean getMoreResults(int current) throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getMoreResults(current); + } + return false; } @Override public ResultSet getGeneratedKeys() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getGeneratedKeys(); + } + return null; } @Override public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - // TODO Auto-generated method stub - return 0; + checkStatement(sql); + + return stmt.executeUpdate(sql, autoGeneratedKeys); } @Override public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - // TODO Auto-generated method stub - return 0; + checkStatement(sql); + return stmt.executeUpdate(sql, columnIndexes); } @Override public int executeUpdate(String sql, String[] columnNames) throws SQLException { - // TODO Auto-generated method stub - return 0; + checkStatement(sql); + return stmt.executeUpdate(sql, columnNames); } @Override public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - // TODO Auto-generated method stub - return false; + checkStatement(sql); + return stmt.execute(sql, autoGeneratedKeys); } @Override public boolean execute(String sql, int[] columnIndexes) throws SQLException { - // TODO Auto-generated method stub - return false; + checkStatement(sql); + return stmt.execute(sql, columnIndexes); } @Override public boolean execute(String sql, String[] columnNames) throws SQLException { - // TODO Auto-generated method stub - return false; + checkStatement(sql); + return stmt.execute(sql, columnNames); } @Override public int getResultSetHoldability() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.getResultSetHoldability(); + } + + if (resultSetHoldability != null) { + return resultSetHoldability; + } + return 0; } @Override public boolean isClosed() throws SQLException { - // TODO Auto-generated method stub + if (stmt != null) { + return stmt.isClosed(); + } return false; } @Override public void setPoolable(boolean poolable) throws SQLException { - // TODO Auto-generated method stub - + throw new SQLException("not supported"); } @Override public boolean isPoolable() throws SQLException { - // TODO Auto-generated method stub return false; }