Commit 7b14fcb8 authored by terrymanu's avatar terrymanu
Browse files

refactor ShardingTransactionEvent

parent a57f03fb
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -25,9 +25,9 @@ import io.shardingsphere.core.jdbc.unsupported.AbstractUnsupportedOperationConne
import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.TransactionTypeHolder;
import io.shardingsphere.transaction.event.LocalTransactionEvent;
import io.shardingsphere.transaction.event.TransactionEvent;
import io.shardingsphere.transaction.event.XATransactionEvent;
import io.shardingsphere.transaction.event.local.LocalTransactionEvent;
import io.shardingsphere.transaction.event.ShardingTransactionEvent;
import io.shardingsphere.transaction.event.xa.XATransactionEvent;

import javax.sql.DataSource;
import java.sql.Connection;
@@ -104,7 +104,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
        EventBusInstance.getInstance().post(createTransactionEvent(TCLType.ROLLBACK));
    }
    
    private TransactionEvent createTransactionEvent(final TCLType tclType) {
    private ShardingTransactionEvent createTransactionEvent(final TCLType tclType) {
        switch (TransactionTypeHolder.get()) {
            case LOCAL:
                return new LocalTransactionEvent(tclType, cachedConnections.values(), autoCommit);
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.TransactionTypeHolder;
import io.shardingsphere.transaction.event.XATransactionEvent;
import io.shardingsphere.transaction.event.xa.XATransactionEvent;

import javax.transaction.Status;
import java.sql.SQLException;
+68 −68
Original line number Diff line number Diff line
@@ -9,71 +9,71 @@

# The example of Sharding rule.

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false
    username: root
    password:
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/ds_1?serverTimezone=UTC&useSSL=false
    username: root
    password:
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65

shardingRule:
  tables:
    t_order:
      actualDataNodes: ds_${0..1}.t_order_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_${order_id % 2}
      keyGeneratorColumnName: order_id
    t_order_item:
      actualDataNodes: ds_${0..1}.t_order_item_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item_${order_id % 2}
  bindingTables:
    - t_order,t_order_item
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds_${user_id % 2}
  defaultTableStrategy:
    none:
  defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

  props:
    # MEMORY_STRICTLY: Proxy holds as many connections as the count of actual tables routed in a database.
    #                  The benefit of this approach is saving memory for Proxy by Stream ResultSet.

    # CONNECTION_STRICTLY: Proxy will release connections after get the overall rows from the ResultSet.
    #                      Meanwhile, the cost of the memory will be increased.
    connection.mode: MEMORY_STRICTLY
    acceptor.size: 16  # The default value is available processors count * 2.
    executor.size: 16  # Infinite by default.
    proxy.transaction.mode: XA
    sql.show: false

orchestration:
  name: orchestration_ds
  type: SHARDING
  overwrite: true
  zookeeper:
    namespace: orchestration
    serverLists: localhost:2181

proxyAuthority:
  username: root
  password: root
#dataSources:
#  ds_0:
#    url: jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false
#    username: root
#    password:
#    autoCommit: true
#    connectionTimeout: 30000
#    idleTimeout: 60000
#    maxLifetime: 1800000
#    maximumPoolSize: 65
#  ds_1:
#    url: jdbc:mysql://127.0.0.1:3306/ds_1?serverTimezone=UTC&useSSL=false
#    username: root
#    password:
#    autoCommit: true
#    connectionTimeout: 30000
#    idleTimeout: 60000
#    maxLifetime: 1800000
#    maximumPoolSize: 65
#
#shardingRule:
#  tables:
#    t_order:
#      actualDataNodes: ds_${0..1}.t_order_${0..1}
#      tableStrategy:
#        inline:
#          shardingColumn: order_id
#          algorithmExpression: t_order_${order_id % 2}
#      keyGeneratorColumnName: order_id
#    t_order_item:
#      actualDataNodes: ds_${0..1}.t_order_item_${0..1}
#      tableStrategy:
#        inline:
#          shardingColumn: order_id
#          algorithmExpression: t_order_item_${order_id % 2}
#  bindingTables:
#    - t_order,t_order_item
#  defaultDatabaseStrategy:
#    inline:
#      shardingColumn: user_id
#      algorithmExpression: ds_${user_id % 2}
#  defaultTableStrategy:
#    none:
#  defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
#
#  props:
#    # MEMORY_STRICTLY: Proxy holds as many connections as the count of actual tables routed in a database.
#    #                  The benefit of this approach is saving memory for Proxy by Stream ResultSet.
#
#    # CONNECTION_STRICTLY: Proxy will release connections after get the overall rows from the ResultSet.
#    #                      Meanwhile, the cost of the memory will be increased.
#    connection.mode: MEMORY_STRICTLY
#    acceptor.size: 16  # The default value is available processors count * 2.
#    executor.size: 16  # Infinite by default.
#    proxy.transaction.mode: XA
#    sql.show: false
#
#orchestration:
#  name: orchestration_ds
#  type: SHARDING
#  overwrite: true
#  zookeeper:
#    namespace: orchestration
#    serverLists: localhost:2181
#
#proxyAuthority:
#  username: root
#  password: root
+7 −24
Original line number Diff line number Diff line
@@ -17,37 +17,20 @@

package io.shardingsphere.transaction.event;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.TCLType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.UUID;

/**
 * Abstract Transaction Event.
 * Sharding transaction event.
 *
 * @author zhaojun
 * @author zhangliang
 */
@RequiredArgsConstructor
@Getter
public abstract class TransactionEvent {
    
    private final String id = UUID.randomUUID().toString();
    
    private final TCLType tclType;
    
    @Setter
    private Exception exception;
public interface ShardingTransactionEvent {
    
    /**
     * Get exception.
     * Get transaction operation type.
     * 
     * @return exception
     * @return transaction operation type
     */
    // TODO why not use sharding exception directly?
    public Optional<? extends Exception> getException() {
        return Optional.fromNullable(exception);
    }
    TCLType getOperationType();
}
+7 −15
Original line number Diff line number Diff line
@@ -15,12 +15,12 @@
 * </p>
 */

package io.shardingsphere.transaction.event;
package io.shardingsphere.transaction.event.local;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.TCLType;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.transaction.event.ShardingTransactionEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.sql.Connection;
import java.util.Collection;
@@ -30,21 +30,13 @@ import java.util.Collection;
 *
 * @author zhaojun
 */
@RequiredArgsConstructor
@Getter
public final class LocalTransactionEvent extends TransactionEvent {
public final class LocalTransactionEvent implements ShardingTransactionEvent {
    
    private final TCLType operationType;
            
    private final Collection<Connection> cachedConnections;
    
    private final boolean autoCommit;
    
    public LocalTransactionEvent(final TCLType tclType, final Collection<Connection> cachedConnections, final boolean autoCommit) {
        super(tclType);
        this.cachedConnections = cachedConnections;
        this.autoCommit = autoCommit;
    }
    
    @Override
    public Optional<ShardingException> getException() {
        return Optional.fromNullable((ShardingException) super.getException().orNull());
    }
}
Loading