-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
206 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...a/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/MasterSlavePreparedStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.jdbc.core.statement; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractPreparedStatementAdapter; | ||
import com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.MasterSlaveConnection; | ||
import com.google.common.base.Preconditions; | ||
import lombok.Getter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
|
||
/** | ||
* PreparedStatement that support master-slave. | ||
* | ||
* @author zhangliang | ||
*/ | ||
@Getter | ||
public final class MasterSlavePreparedStatement extends AbstractPreparedStatementAdapter { | ||
|
||
private final MasterSlaveConnection connection; | ||
|
||
private final Collection<PreparedStatement> routedStatements = new LinkedList<>(); | ||
|
||
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql) throws SQLException { | ||
this(connection, sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT); | ||
} | ||
|
||
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { | ||
this(connection, sql, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT); | ||
} | ||
|
||
public MasterSlavePreparedStatement( | ||
final MasterSlaveConnection connection, final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { | ||
this.connection = connection; | ||
for (Connection each : connection.getConnection(sql)) { | ||
PreparedStatement preparedStatement = each.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); | ||
routedStatements.add(preparedStatement); | ||
} | ||
} | ||
|
||
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int autoGeneratedKeys) throws SQLException { | ||
this.connection = connection; | ||
for (Connection each : connection.getConnection(sql)) { | ||
PreparedStatement preparedStatement = each.prepareStatement(sql, autoGeneratedKeys); | ||
routedStatements.add(preparedStatement); | ||
} | ||
} | ||
|
||
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int[] columnIndexes) throws SQLException { | ||
this.connection = connection; | ||
for (Connection each : connection.getConnection(sql)) { | ||
PreparedStatement preparedStatement = each.prepareStatement(sql, columnIndexes); | ||
routedStatements.add(preparedStatement); | ||
} | ||
} | ||
|
||
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final String[] columnNames) throws SQLException { | ||
this.connection = connection; | ||
for (Connection each : connection.getConnection(sql)) { | ||
PreparedStatement preparedStatement = each.prepareStatement(sql, columnNames); | ||
routedStatements.add(preparedStatement); | ||
} | ||
} | ||
|
||
@Override | ||
public ResultSet executeQuery() throws SQLException { | ||
Preconditions.checkState(1 == routedStatements.size()); | ||
return routedStatements.iterator().next().executeQuery(); | ||
} | ||
|
||
@Override | ||
public int executeUpdate() throws SQLException { | ||
Preconditions.checkState(1 == routedStatements.size()); | ||
return routedStatements.iterator().next().executeUpdate(); | ||
} | ||
|
||
@Override | ||
public boolean execute() throws SQLException { | ||
boolean result = false; | ||
for (PreparedStatement each : routedStatements) { | ||
result = each.execute(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void clearBatch() throws SQLException { | ||
for (PreparedStatement each : routedStatements) { | ||
each.clearBatch(); | ||
} | ||
} | ||
|
||
@Override | ||
public void addBatch() throws SQLException { | ||
for (PreparedStatement each : routedStatements) { | ||
each.addBatch(); | ||
} | ||
} | ||
|
||
@Override | ||
public int[] executeBatch() throws SQLException { | ||
Preconditions.checkState(1 == routedStatements.size()); | ||
return routedStatements.iterator().next().executeBatch(); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet() throws SQLException { | ||
Preconditions.checkState(1 == routedStatements.size()); | ||
return routedStatements.iterator().next().getResultSet(); | ||
} | ||
|
||
@Override | ||
public ResultSet getGeneratedKeys() throws SQLException { | ||
Preconditions.checkState(1 == routedStatements.size()); | ||
return routedStatements.iterator().next().getGeneratedKeys(); | ||
} | ||
|
||
@Override | ||
public int getResultSetHoldability() throws SQLException { | ||
return routedStatements.iterator().next().getResultSetHoldability(); | ||
} | ||
|
||
@Override | ||
public int getResultSetConcurrency() throws SQLException { | ||
return routedStatements.iterator().next().getResultSetConcurrency(); | ||
} | ||
|
||
@Override | ||
public int getResultSetType() throws SQLException { | ||
return routedStatements.iterator().next().getResultSetType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.