提交 e1f241cb 编写于 作者: T terrymanu

remove TransactionContext

上级 0fb35825
......@@ -24,10 +24,7 @@ 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.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.api.local.LocalTransactionManager;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.TransactionTypeHolder;
import io.shardingsphere.transaction.common.event.LocalTransactionEvent;
import io.shardingsphere.transaction.common.event.TransactionEvent;
import io.shardingsphere.transaction.common.event.TransactionEventFactory;
......@@ -67,8 +64,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(ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(transactionType), transactionType));
if (cachedConnections.containsKey(dataSourceName)) {
return cachedConnections.get(dataSourceName);
}
......@@ -94,7 +89,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void setAutoCommit(final boolean autoCommit) {
this.autoCommit = autoCommit;
TransactionContextHolder.set(new TransactionContext(new LocalTransactionManager(), TransactionType.LOCAL));
TransactionTypeHolder.set(TransactionType.LOCAL);
recordMethodInvocation(Connection.class, "setAutoCommit", new Class[] {boolean.class}, new Object[] {autoCommit});
EventBusInstance.getInstance().post(buildTransactionEvent(TCLType.BEGIN));
}
......@@ -114,7 +109,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
closed = true;
HintManagerHolder.clear();
MasterVisitedManager.clear();
TransactionContextHolder.clear();
TransactionTypeHolder.clear();
Collection<SQLException> exceptions = new LinkedList<>();
for (Connection each : cachedConnections.values()) {
try {
......
......@@ -19,12 +19,8 @@ package io.shardingsphere.core.jdbc.adapter;
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.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import lombok.Getter;
import javax.sql.DataSource;
......@@ -42,8 +38,6 @@ import java.util.logging.Logger;
public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource {
static {
TransactionType transactionType = TransactionContextHolder.get().getTransactionType();
TransactionContextHolder.set(new TransactionContext(ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(transactionType), transactionType));
JDBCListenerRegister.register();
}
......
......@@ -22,8 +22,7 @@ import io.shardingsphere.core.constant.TCLType;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.proxy.config.RuleRegistry;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.TransactionTypeHolder;
import io.shardingsphere.transaction.common.event.XaTransactionEvent;
import javax.transaction.Status;
......@@ -46,7 +45,7 @@ public final class XaTransactionEngine extends TransactionEngine {
public boolean execute() throws SQLException {
Optional<TCLType> tclType = parseSQL();
if (tclType.isPresent() && isInTransaction(tclType.get())) {
TransactionContextHolder.set(new TransactionContext(RULE_REGISTRY.getTransactionManager(), TransactionType.XA));
TransactionTypeHolder.set(TransactionType.XA);
EventBusInstance.getInstance().post(new XaTransactionEvent(tclType.get(), getSql()));
return true;
}
......
......@@ -35,8 +35,7 @@ import io.shardingsphere.jdbc.orchestration.internal.eventbus.ProxyEventBusEvent
import io.shardingsphere.proxy.backend.jdbc.datasource.JDBCBackendDataSource;
import io.shardingsphere.transaction.api.ShardingTransactionManager;
import io.shardingsphere.transaction.api.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.TransactionTypeHolder;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
......@@ -109,7 +108,7 @@ public final class RuleRegistry {
connectionMode = ConnectionMode.valueOf(shardingProperties.<String>getValue(ShardingPropertiesConstant.CONNECTION_MODE));
transactionType = TransactionType.valueOf(shardingProperties.<String>getValue(ShardingPropertiesConstant.PROXY_TRANSACTION_MODE));
transactionManager = ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(transactionType);
TransactionContextHolder.set(new TransactionContext(transactionManager, transactionType));
TransactionTypeHolder.set(transactionType);
acceptorSize = shardingProperties.getValue(ShardingPropertiesConstant.ACCEPTOR_SIZE);
executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
// TODO :jiaqi force off use NIO for backend, this feature is not complete yet
......
/*
* 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.transaction.common;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.transaction.api.ShardingTransactionManager;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* Hold Transaction Context.
*
* @author zhaojun
*/
@NoArgsConstructor
@Getter
public final class TransactionContext {
private ShardingTransactionManager transactionManager;
private TransactionType transactionType = TransactionType.LOCAL;
public TransactionContext(final ShardingTransactionManager transactionManager, final TransactionType transactionType) {
this.transactionManager = transactionManager;
this.transactionType = transactionType;
}
}
......@@ -17,41 +17,44 @@
package io.shardingsphere.transaction.common;
import io.shardingsphere.core.constant.TransactionType;
/**
* Hold transaction context for current thread.
* Hold transaction type for current thread.
*
* @author zhaojun
* @author zhangliang
*/
public final class TransactionContextHolder {
public final class TransactionTypeHolder {
private static final ThreadLocal<TransactionContext> CONTEXT = new ThreadLocal<TransactionContext>() {
private static final ThreadLocal<TransactionType> CONTEXT = new ThreadLocal<TransactionType>() {
@Override
protected TransactionContext initialValue() {
return new TransactionContext();
protected TransactionType initialValue() {
return TransactionType.LOCAL;
}
};
/**
* Get transaction context for current thread.
* Get transaction type for current thread.
*
* @return TransactionContext
* @return transaction type
*/
public static TransactionContext get() {
public static TransactionType get() {
return CONTEXT.get();
}
/**
* Set transaction context for current thread.
* Set transaction type for current thread.
*
* @param context Transaction context
* @param transactionType transaction type
*/
public static void set(final TransactionContext context) {
CONTEXT.set(context);
public static void set(final TransactionType transactionType) {
CONTEXT.set(transactionType);
}
/**
* Clear transaction context for current thread.
* Clear transaction type for current thread.
*/
public static void clear() {
CONTEXT.remove();
......
......@@ -18,7 +18,7 @@
package io.shardingsphere.transaction.common.event;
import io.shardingsphere.core.constant.TCLType;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.TransactionTypeHolder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -37,7 +37,7 @@ public final class TransactionEventFactory {
* @return transaction event
*/
public static TransactionEvent create(final TCLType tclType) {
switch (TransactionContextHolder.get().getTransactionType()) {
switch (TransactionTypeHolder.get()) {
case LOCAL:
return new LocalTransactionEvent(tclType);
case XA:
......
......@@ -21,7 +21,8 @@ import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.api.ShardingTransactionManager;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.api.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.common.TransactionTypeHolder;
import io.shardingsphere.transaction.common.event.TransactionEvent;
import java.sql.SQLException;
......@@ -49,16 +50,16 @@ public final class TransactionListener {
@Subscribe
@AllowConcurrentEvents
public void listen(final TransactionEvent transactionEvent) throws SQLException {
ShardingTransactionManager transactionManager = TransactionContextHolder.get().getTransactionManager();
ShardingTransactionManager shardingTransactionManager = ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionTypeHolder.get());
switch (transactionEvent.getTclType()) {
case BEGIN:
transactionManager.begin(transactionEvent);
shardingTransactionManager.begin(transactionEvent);
break;
case COMMIT:
transactionManager.commit(transactionEvent);
shardingTransactionManager.commit(transactionEvent);
break;
case ROLLBACK:
transactionManager.rollback(transactionEvent);
shardingTransactionManager.rollback(transactionEvent);
break;
default:
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册