View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.transaction.spi;
19  
20  import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
21  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
22  import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
23  import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
24  import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPI;
25  import org.apache.shardingsphere.sql.parser.statement.core.enums.TransactionIsolationLevel;
26  
27  import java.sql.Connection;
28  import java.sql.SQLException;
29  import java.util.Collection;
30  
31  /**
32   * ShardingSphere transaction hook.
33   * 
34   * @param <T> type of rule
35   */
36  @SingletonSPI
37  public interface TransactionHook<T extends ShardingSphereRule> extends OrderedSPI<T> {
38      
39      /**
40       * Process before opening the transaction.
41       *
42       * @param rule rule
43       * @param databaseType database type
44       * @param transactionContext transaction context
45       */
46      void beforeBegin(T rule, DatabaseType databaseType, TransactionConnectionContext transactionContext);
47      
48      /**
49       * Process after opening the transaction.
50       *
51       * @param rule rule
52       * @param databaseType database type
53       * @param transactionContext transaction context
54       */
55      void afterBegin(T rule, DatabaseType databaseType, TransactionConnectionContext transactionContext);
56      
57      /**
58       * Process after connection is created.
59       *
60       * @param rule rule
61       * @param databaseType database type
62       * @param connections connections
63       * @param transactionContext transaction context
64       * @throws SQLException SQL exception
65       */
66      void afterCreateConnections(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext) throws SQLException;
67      
68      /**
69       * Process before executing SQL.
70       *
71       * @param rule rule
72       * @param databaseType database type
73       * @param connections connections
74       * @param transactionContext transaction context
75       * @param isolationLevel isolation level
76       * @throws SQLException SQL exception
77       */
78      void beforeExecuteSQL(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext,
79                            TransactionIsolationLevel isolationLevel) throws SQLException;
80      
81      /**
82       * Process before committing the transaction.
83       *
84       * @param rule rule
85       * @param databaseType database type
86       * @param connections connections
87       * @param transactionContext transaction context
88       * @throws SQLException SQL exception
89       */
90      void beforeCommit(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext) throws SQLException;
91      
92      /**
93       * Process after committing the transaction.
94       *
95       * @param rule rule
96       * @param databaseType database type
97       * @param connections connections
98       * @param transactionContext transaction context
99       */
100     void afterCommit(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext);
101     
102     /**
103      * Whether to need lock when transaction committed.
104      *
105      * @param rule rule
106      * @return need lock or not
107      */
108     boolean isNeedLockWhenCommit(T rule);
109     
110     /**
111      * Process before rolling back the transaction.
112      *
113      * @param rule rule
114      * @param databaseType database type
115      * @param connections connections
116      * @param transactionContext transaction context
117      */
118     void beforeRollback(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext);
119     
120     /**
121      * Process after rolling back the transaction.
122      *
123      * @param rule rule
124      * @param databaseType database type
125      * @param connections connections
126      * @param transactionContext transaction context
127      */
128     void afterRollback(T rule, DatabaseType databaseType, Collection<Connection> connections, TransactionConnectionContext transactionContext);
129 }