Skip to content

Commit

Permalink
Merge pull request #19 from sharding-sphere/dev
Browse files Browse the repository at this point in the history
update from origin
  • Loading branch information
beckhampu authored Aug 14, 2018
2 parents 2e4c47f + 18fcde4 commit ff2c137
Show file tree
Hide file tree
Showing 59 changed files with 704 additions and 723 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingsphere.core.constant;
package io.shardingsphere.core.constant.properties;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* </p>
*/

package io.shardingsphere.core.constant;
package io.shardingsphere.core.constant.properties;

import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.core.constant.transaction.TransactionType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <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 io.shardingsphere.core.constant.transaction;

import com.google.common.base.Optional;

/**
* Transaction operation type.
*
* @author zhaojun
*/
public enum TransactionOperationType {

BEGIN, COMMIT, ROLLBACK;

/**
* Get operation type.
*
* @param sql SQL
* @return transaction operation type
*/
// TODO :hongjun move to TCLParser, need parse comment etc
public static Optional<TransactionOperationType> getOperationType(final String sql) {
switch (sql.toUpperCase()) {
case "BEGIN":
case "START TRANSACTION":
case "SET AUTOCOMMIT=0":
return Optional.of(TransactionOperationType.BEGIN);
case "COMMIT":
return Optional.of(TransactionOperationType.COMMIT);
case "ROLLBACK":
return Optional.of(TransactionOperationType.ROLLBACK);
default:
return Optional.absent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingsphere.core.constant;
package io.shardingsphere.core.constant.transaction;

/**
* Transaction type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.shardingsphere.core.constant;

import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.shardingsphere.core.constant;

import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.junit.Before;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package io.shardingsphere.jdbc.spring.boot.type;

import io.shardingsphere.core.api.ConfigMapContext;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.core.ShardingContext;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.jdbc.spring.boot.util.EmbedTestingServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.shardingsphere.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationMasterSlaveDataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import io.shardingsphere.core.api.config.strategy.InlineShardingStrategyConfiguration;
import io.shardingsphere.core.api.config.strategy.NoneShardingStrategyConfiguration;
import io.shardingsphere.core.api.config.strategy.StandardShardingStrategyConfiguration;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.BindingTableRule;
import io.shardingsphere.core.rule.DataNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package io.shardingsphere.jdbc.spring.boot.type;

import io.shardingsphere.core.api.ConfigMapContext;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.core.ShardingContext;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import io.shardingsphere.core.api.config.strategy.InlineShardingStrategyConfiguration;
import io.shardingsphere.core.api.config.strategy.NoneShardingStrategyConfiguration;
import io.shardingsphere.core.api.config.strategy.StandardShardingStrategyConfiguration;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.BindingTableRule;
import io.shardingsphere.core.rule.DataNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@
package io.shardingsphere.core.jdbc.adapter;

import com.google.common.base.Preconditions;
import io.shardingsphere.core.constant.TCLType;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.constant.transaction.TransactionOperationType;
import io.shardingsphere.core.hint.HintManagerHolder;
import io.shardingsphere.core.jdbc.unsupported.AbstractUnsupportedOperationConnection;
import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.api.ShardingTransactionManagerFactory;
import io.shardingsphere.transaction.api.local.LocalTransactionManager;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.event.LocalTransactionEvent;
import io.shardingsphere.transaction.common.event.TransactionEvent;
import io.shardingsphere.transaction.common.event.TransactionEventFactory;
import io.shardingsphere.transaction.TransactionTypeHolder;
import io.shardingsphere.transaction.event.ShardingTransactionEvent;
import io.shardingsphere.transaction.event.local.LocalTransactionEvent;
import io.shardingsphere.transaction.event.xa.XATransactionEvent;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -67,8 +63,6 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
* @throws SQLException SQL exception
*/
public final Connection getConnection(final String dataSourceName) throws SQLException {
TransactionType transactionType = TransactionContextHolder.get().getTransactionType();
TransactionContextHolder.set(new TransactionContext(ShardingTransactionManagerFactory.getShardingTransactionManager(transactionType), transactionType));
if (cachedConnections.containsKey(dataSourceName)) {
return cachedConnections.get(dataSourceName);
}
Expand All @@ -94,27 +88,38 @@ public final boolean getAutoCommit() {
@Override
public final void setAutoCommit(final boolean autoCommit) {
this.autoCommit = autoCommit;
TransactionContextHolder.set(new TransactionContext(new LocalTransactionManager(), TransactionType.LOCAL));
recordMethodInvocation(Connection.class, "setAutoCommit", new Class[] {boolean.class}, new Object[] {autoCommit});
EventBusInstance.getInstance().post(buildTransactionEvent(TCLType.BEGIN));
EventBusInstance.getInstance().post(createTransactionEvent(TransactionOperationType.BEGIN));
}

@Override
public final void commit() {
EventBusInstance.getInstance().post(buildTransactionEvent(TCLType.COMMIT));
EventBusInstance.getInstance().post(createTransactionEvent(TransactionOperationType.COMMIT));
}

@Override
public final void rollback() {
EventBusInstance.getInstance().post(buildTransactionEvent(TCLType.ROLLBACK));
EventBusInstance.getInstance().post(createTransactionEvent(TransactionOperationType.ROLLBACK));
}

private ShardingTransactionEvent createTransactionEvent(final TransactionOperationType operationType) {
switch (TransactionTypeHolder.get()) {
case LOCAL:
return new LocalTransactionEvent(operationType, cachedConnections.values(), autoCommit);
case XA:
return new XATransactionEvent(operationType);
case BASE:
default:
throw new UnsupportedOperationException(TransactionTypeHolder.get().name());
}
}

@Override
public final void close() throws SQLException {
closed = true;
HintManagerHolder.clear();
MasterVisitedManager.clear();
TransactionContextHolder.clear();
TransactionTypeHolder.clear();
Collection<SQLException> exceptions = new LinkedList<>();
for (Connection each : cachedConnections.values()) {
try {
Expand Down Expand Up @@ -181,14 +186,4 @@ public final int getHoldability() {
@Override
public final void setHoldability(final int holdability) {
}

private TransactionEvent buildTransactionEvent(final TCLType tclType) {
TransactionEvent result = TransactionEventFactory.create(tclType);
if (result instanceof LocalTransactionEvent) {
LocalTransactionEvent localTransactionEvent = (LocalTransactionEvent) result;
localTransactionEvent.setCachedConnections(cachedConnections.values());
localTransactionEvent.setAutoCommit(autoCommit);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@

import com.google.common.base.Preconditions;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import io.shardingsphere.core.listener.JDBCListenerRegister;
import io.shardingsphere.transaction.api.ShardingTransactionManagerFactory;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import lombok.Getter;

import javax.sql.DataSource;
Expand All @@ -42,8 +38,6 @@
public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource {

static {
TransactionType transactionType = TransactionContextHolder.get().getTransactionType();
TransactionContextHolder.set(new TransactionContext(ShardingTransactionManagerFactory.getShardingTransactionManager(transactionType), transactionType));
JDBCListenerRegister.register();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import io.shardingsphere.core.api.ConfigMapContext;
import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter;
import io.shardingsphere.core.jdbc.core.connection.MasterSlaveConnection;
import io.shardingsphere.core.rule.MasterSlaveRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.core.constant.ShardingProperties;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.executor.ExecutorEngine;
import io.shardingsphere.core.executor.type.connection.ConnectionStrictlyExecutorEngine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package io.shardingsphere.core.listener;

import io.shardingsphere.transaction.common.listener.TransactionListener;
import io.shardingsphere.transaction.listener.local.LocalTransactionListener;
import io.shardingsphere.transaction.listener.xa.XATransactionListener;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

Expand All @@ -33,6 +34,7 @@ public final class JDBCListenerRegister {
* Register all listeners.
*/
public static void register() {
new TransactionListener().register();
new LocalTransactionListener().register();
new XATransactionListener().register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.api.config.TableRuleConfiguration;
import io.shardingsphere.core.constant.ShardingPropertiesConstant;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.executor.ExecutorEngine;
import io.shardingsphere.core.rule.ShardingRule;
import org.junit.Test;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void assertGetDatabaseProductNameForMasterSlave() throws SQLException {

private void assertDatabaseProductName(final Map<String, DataSource> dataSourceMap, final Connection... connections) throws SQLException {
try {
createShardingDataSource(dataSourceMap).getDatabaseType();
assertThat(createShardingDataSource(dataSourceMap).getDatabaseType(), is(DatabaseType.H2));
} finally {
for (Connection each : connections) {
verify(each, atLeast(1)).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.constant.transaction.TransactionType;
import io.shardingsphere.core.merger.MergeEngineFactory;
import io.shardingsphere.core.merger.MergedResult;
import io.shardingsphere.core.metadata.table.executor.TableMetaDataLoader;
Expand All @@ -44,6 +44,7 @@
import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket;
import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket;
import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket;
import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry;
import lombok.RequiredArgsConstructor;

import javax.transaction.Status;
Expand Down Expand Up @@ -100,7 +101,8 @@ private CommandResponsePackets execute(final SQLRouteResult routeResult) throws
}

private boolean isUnsupportedXA(final SQLType sqlType) throws SQLException {
return TransactionType.XA == RULE_REGISTRY.getTransactionType() && SQLType.DDL == sqlType && Status.STATUS_NO_TRANSACTION != RULE_REGISTRY.getTransactionManager().getStatus();
return TransactionType.XA == RULE_REGISTRY.getTransactionType() && SQLType.DDL == sqlType
&& Status.STATUS_NO_TRANSACTION != ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionType.XA).getStatus();
}

private CommandResponsePackets merge(final SQLStatement sqlStatement) throws SQLException {
Expand Down
Loading

0 comments on commit ff2c137

Please sign in to comment.