Commit a57f03fb authored by terrymanu's avatar terrymanu
Browse files

add generic type for ShardingTransactionManager

parent e73b3e15
Loading
Loading
Loading
Loading
+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
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public abstract class ShardingTransactionListenerAdapter<T extends TransactionEv
        EventBusInstance.getInstance().register(this);
    }
    
    protected final void doTransaction(final ShardingTransactionManager shardingTransactionManager, final TransactionEvent transactionEvent) throws SQLException {
    @SuppressWarnings("unchecked")
    protected final void doTransaction(final ShardingTransactionManager shardingTransactionManager, final T transactionEvent) throws SQLException {
        switch (transactionEvent.getTclType()) {
            case BEGIN:
                shardingTransactionManager.begin(transactionEvent);
+6 −4
Original line number Diff line number Diff line
@@ -25,8 +25,10 @@ import java.sql.SQLException;
 *
 * @author zhaojun
 * @author zhangliang
 * 
 * @param <T> transaction event type
 */
public interface ShardingTransactionManager {
public interface ShardingTransactionManager<T extends TransactionEvent> {
    
    /**
     * Begin transaction.
@@ -34,7 +36,7 @@ public interface ShardingTransactionManager {
     * @param transactionEvent transaction event
     * @throws SQLException SQL exception
     */
    void begin(TransactionEvent transactionEvent) throws SQLException;
    void begin(T transactionEvent) throws SQLException;
    
    /**
     * Commit transaction.
@@ -42,7 +44,7 @@ public interface ShardingTransactionManager {
     * @param transactionEvent transaction event
     * @throws SQLException SQL exception
     */
    void commit(TransactionEvent transactionEvent) throws SQLException;
    void commit(T transactionEvent) throws SQLException;
    
    /**
     * Rollback transaction.
@@ -50,7 +52,7 @@ public interface ShardingTransactionManager {
     * @param transactionEvent transaction event
     * @throws SQLException SQL exception
     */
    void rollback(TransactionEvent transactionEvent) throws SQLException;
    void rollback(T transactionEvent) throws SQLException;
    
    /**
     * Obtain the status of the transaction associated with the current thread.
+9 −13
Original line number Diff line number Diff line
@@ -17,9 +17,8 @@

package io.shardingsphere.transaction.manager.local;

import io.shardingsphere.transaction.manager.ShardingTransactionManager;
import io.shardingsphere.transaction.event.TransactionEvent;
import io.shardingsphere.transaction.event.LocalTransactionEvent;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;

import javax.transaction.Status;
import java.sql.Connection;
@@ -32,15 +31,14 @@ import java.util.LinkedList;
 *
 * @author zhaojun
 */
public final class LocalTransactionManager implements ShardingTransactionManager {
public final class LocalTransactionManager implements ShardingTransactionManager<LocalTransactionEvent> {
    
    @Override
    public void begin(final TransactionEvent transactionEvent) throws SQLException {
        LocalTransactionEvent localTransactionEvent = (LocalTransactionEvent) transactionEvent;
    public void begin(final LocalTransactionEvent event) throws SQLException {
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Connection each : localTransactionEvent.getCachedConnections()) {
        for (Connection each : event.getCachedConnections()) {
            try {
                each.setAutoCommit(localTransactionEvent.isAutoCommit());
                each.setAutoCommit(event.isAutoCommit());
            } catch (final SQLException ex) {
                exceptions.add(ex);
            }
@@ -49,10 +47,9 @@ public final class LocalTransactionManager implements ShardingTransactionManager
    }
    
    @Override
    public void commit(final TransactionEvent transactionEvent) throws SQLException {
        LocalTransactionEvent localTransactionEvent = (LocalTransactionEvent) transactionEvent;
    public void commit(final LocalTransactionEvent event) throws SQLException {
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Connection each : localTransactionEvent.getCachedConnections()) {
        for (Connection each : event.getCachedConnections()) {
            try {
                each.commit();
            } catch (final SQLException ex) {
@@ -63,10 +60,9 @@ public final class LocalTransactionManager implements ShardingTransactionManager
    }
    
    @Override
    public void rollback(final TransactionEvent transactionEvent) throws SQLException {
        LocalTransactionEvent localTransactionEvent = (LocalTransactionEvent) transactionEvent;
    public void rollback(final LocalTransactionEvent event) throws SQLException {
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Connection each : localTransactionEvent.getCachedConnections()) {
        for (Connection each : event.getCachedConnections()) {
            try {
                each.rollback();
            } catch (final SQLException ex) {
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package io.shardingsphere.transaction.manager.xa;

import io.shardingsphere.transaction.event.XATransactionEvent;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;

/**
@@ -24,5 +25,5 @@ import io.shardingsphere.transaction.manager.ShardingTransactionManager;
 *
 * @author zhangliang
 */
public interface XATransactionManager extends ShardingTransactionManager {
public interface XATransactionManager extends ShardingTransactionManager<XATransactionEvent> {
}
Loading