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.infra.session.connection.transaction.TransactionConnectionContext;
21  import org.apache.shardingsphere.infra.lock.LockContext;
22  import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
23  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
24  import org.apache.shardingsphere.sql.parser.sql.common.enums.TransactionIsolationLevel;
25  
26  import java.sql.Connection;
27  import java.sql.SQLException;
28  import java.util.Collection;
29  
30  /**
31   * ShardingSphere transaction hook.
32   */
33  @SingletonSPI
34  public interface TransactionHook extends TypedSPI {
35      
36      /**
37       * Process before opening the transaction.
38       *
39       * @param transactionContext transaction context
40       */
41      void beforeBegin(TransactionConnectionContext transactionContext);
42      
43      /**
44       * Process after opening the transaction.
45       *
46       * @param transactionContext transaction context
47       */
48      void afterBegin(TransactionConnectionContext transactionContext);
49      
50      /**
51       * Process after connection is created.
52       *
53       * @param connections connections
54       * @param transactionContext transaction context
55       * @throws SQLException SQL exception
56       */
57      void afterCreateConnections(Collection<Connection> connections, TransactionConnectionContext transactionContext) throws SQLException;
58      
59      /**
60       * Process before executing sql.
61       *
62       * @param connections connections
63       * @param transactionContext transaction context
64       * @param isolationLevel isolation level
65       * @throws SQLException SQL exception
66       */
67      void beforeExecuteSQL(Collection<Connection> connections, TransactionConnectionContext transactionContext, TransactionIsolationLevel isolationLevel) throws SQLException;
68      
69      /**
70       * Process before committing the transaction.
71       *
72       * @param connections connections
73       * @param transactionContext transaction context
74       * @param lockContext lock context
75       * @throws SQLException SQL exception
76       */
77      @SuppressWarnings("rawtypes")
78      void beforeCommit(Collection<Connection> connections, TransactionConnectionContext transactionContext, LockContext lockContext) throws SQLException;
79      
80      /**
81       * Process after committing the transaction.
82       *
83       * @param connections connections
84       * @param transactionContext transaction context
85       * @param lockContext lock context
86       * @throws SQLException SQL exception
87       */
88      @SuppressWarnings("rawtypes")
89      void afterCommit(Collection<Connection> connections, TransactionConnectionContext transactionContext, LockContext lockContext) throws SQLException;
90      
91      /**
92       * Process before rolling back the transaction.
93       *
94       * @param connections connections
95       * @param transactionContext transaction context
96       * @throws SQLException SQL exception
97       */
98      void beforeRollback(Collection<Connection> connections, TransactionConnectionContext transactionContext) throws SQLException;
99      
100     /**
101      * Process after rolling back the transaction.
102      *
103      * @param connections connections
104      * @param transactionContext transaction context
105      * @throws SQLException SQL exception
106      */
107     void afterRollback(Collection<Connection> connections, TransactionConnectionContext transactionContext) throws SQLException;
108 }