diff --git a/sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingProperties.java b/sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingProperties.java similarity index 98% rename from sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingProperties.java rename to sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingProperties.java index c56bbb8ca179f..d884d2352f979 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingProperties.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingProperties.java @@ -15,7 +15,7 @@ *
*/ -package io.shardingsphere.core.constant; +package io.shardingsphere.core.constant.properties; import com.google.common.base.Joiner; import com.google.common.base.Strings; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingPropertiesConstant.java b/sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingPropertiesConstant.java similarity index 95% rename from sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingPropertiesConstant.java rename to sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingPropertiesConstant.java index 75c7534d2d43c..4ab5ffe23dcfd 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/constant/ShardingPropertiesConstant.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/constant/properties/ShardingPropertiesConstant.java @@ -15,8 +15,10 @@ * */ -package io.shardingsphere.core.constant; +package io.shardingsphere.core.constant.properties; +import io.shardingsphere.core.constant.ConnectionMode; +import io.shardingsphere.core.constant.transaction.TransactionType; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/constant/transaction/TransactionOperationType.java b/sharding-core/src/main/java/io/shardingsphere/core/constant/transaction/TransactionOperationType.java new file mode 100644 index 0000000000000..e528651fa0771 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/constant/transaction/TransactionOperationType.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *+ * 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. + *
+ */ + +package io.shardingsphere.core.constant.transaction; + +import com.google.common.base.Optional; + +/** + * Transaction operation type. + * + * @author zhaojun + */ +public enum TransactionOperationType { + + BEGIN, COMMIT, ROLLBACK; + + /** + * Get operation type. + * + * @param sql SQL + * @return transaction operation type + */ + // TODO :hongjun move to TCLParser, need parse comment etc + public static Optional+ * 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. + *
+ */ + +package io.shardingsphere.proxy.backend.jdbc.transaction; + +import com.google.common.base.Optional; +import io.shardingsphere.core.constant.transaction.TransactionOperationType; +import io.shardingsphere.core.constant.transaction.TransactionType; +import io.shardingsphere.core.util.EventBusInstance; +import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry; +import io.shardingsphere.transaction.TransactionTypeHolder; +import io.shardingsphere.transaction.event.xa.XATransactionEvent; +import lombok.RequiredArgsConstructor; + +import javax.transaction.Status; +import java.sql.SQLException; + +/** + * Execute XA transaction intercept. + * + * @author zhaojun + */ +@RequiredArgsConstructor +public final class XATransactionEngine implements TransactionEngine { + + private final String sql; + + @Override + public boolean execute() throws SQLException { + Optional- * 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. - *
- */ - -package io.shardingsphere.proxy.backend.jdbc.transaction; - -import com.google.common.base.Optional; -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.event.XaTransactionEvent; - -import javax.transaction.Status; -import java.sql.SQLException; - -/** - * Execute XA transaction intercept. - * - * @author zhaojun - */ -public final class XaTransactionEngine extends TransactionEngine { - - private static final RuleRegistry RULE_REGISTRY = RuleRegistry.getInstance(); - - public XaTransactionEngine(final String sql) { - super(sql); - } - - @Override - public boolean execute() throws SQLException { - Optional-# 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. -#
-# - com.atomikos.icatch.serial_jta_transactions=false com.atomikos.icatch.default_jta_timeout = 1000000 com.atomikos.icatch.max_actives = 10000 diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContextHolder.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/TransactionTypeHolder.java similarity index 53% rename from sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContextHolder.java rename to sharding-transaction/src/main/java/io/shardingsphere/transaction/TransactionTypeHolder.java index d14285f1f4400..8f2022a560aa0 100644 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContextHolder.java +++ b/sharding-transaction/src/main/java/io/shardingsphere/transaction/TransactionTypeHolder.java @@ -15,43 +15,46 @@ * */ -package io.shardingsphere.transaction.common; +package io.shardingsphere.transaction; + +import io.shardingsphere.core.constant.transaction.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- * 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. - *
- */ - -package io.shardingsphere.transaction.api; - -import io.shardingsphere.core.constant.TransactionType; -import io.shardingsphere.transaction.api.local.LocalTransactionManager; -import io.shardingsphere.transaction.api.xa.XATransactionManagerSPILoader; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * Sharding transaction manager factory. - * - * @author zhangliang - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class ShardingTransactionManagerFactory { - - /** - * Get sharding transaction manager. - * - * @param transactionType transaction type - * @return sharding transaction manager - */ - public static ShardingTransactionManager getShardingTransactionManager(final TransactionType transactionType) { - switch (transactionType) { - case LOCAL: - return new LocalTransactionManager(); - case XA: - return XATransactionManagerSPILoader.getInstance().getTransactionManager(); - case BASE: - default: - return null; - } - } -} diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/api/xa/AtomikosTransactionManager.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/api/xa/AtomikosTransactionManager.java deleted file mode 100644 index 7977d51cc4845..0000000000000 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/api/xa/AtomikosTransactionManager.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * 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. - *
- */ - -package io.shardingsphere.transaction.api.xa; - -import com.atomikos.icatch.jta.UserTransactionManager; -import io.shardingsphere.core.exception.ShardingException; -import io.shardingsphere.transaction.common.event.TransactionEvent; - -import javax.transaction.HeuristicMixedException; -import javax.transaction.HeuristicRollbackException; -import javax.transaction.NotSupportedException; -import javax.transaction.RollbackException; -import javax.transaction.SystemException; -import java.sql.SQLException; - -/** - * Atomikos XA transaction manager. - * - * @author zhaojun - */ -public final class AtomikosTransactionManager implements XATransactionManager { - - private static final UserTransactionManager TRANSACTION_MANAGER = AtomikosUserTransaction.getInstance(); - - static { - try { - TRANSACTION_MANAGER.init(); - } catch (final SystemException ex) { - throw new ShardingException(ex); - } - } - - @Override - public void begin(final TransactionEvent transactionEvent) throws SQLException { - try { - TRANSACTION_MANAGER.begin(); - } catch (final SystemException | NotSupportedException ex) { - throw new SQLException(ex); - } - } - - @Override - public void commit(final TransactionEvent transactionEvent) throws SQLException { - try { - TRANSACTION_MANAGER.commit(); - } catch (final RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException ex) { - throw new SQLException(ex); - } - } - - @Override - public void rollback(final TransactionEvent transactionEvent) throws SQLException { - try { - TRANSACTION_MANAGER.rollback(); - } catch (final SystemException ex) { - throw new SQLException(ex); - } - } - - @Override - public int getStatus() throws SQLException { - try { - return TRANSACTION_MANAGER.getStatus(); - } catch (final SystemException ex) { - throw new SQLException(ex); - } - } -} diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContext.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContext.java deleted file mode 100644 index 51606268322ae..0000000000000 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/TransactionContext.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * 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. - *
- */ - -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; - } -} diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEvent.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEvent.java deleted file mode 100644 index 5c02c1b41c74a..0000000000000 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEvent.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * 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. - *
- */ - -package io.shardingsphere.transaction.common.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. - * - * @author zhaojun - */ -@RequiredArgsConstructor -@Getter -public abstract class TransactionEvent { - - private final String id = UUID.randomUUID().toString(); - - private final TCLType tclType; - - @Setter - private Exception exception; - - /** - * Get exception. - * - * @return exception - */ - // TODO why not use sharding exception directly? - public Optional extends Exception> getException() { - return Optional.fromNullable(exception); - } -} diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEventFactory.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEventFactory.java deleted file mode 100644 index ad696c81a32f6..0000000000000 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/TransactionEventFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * 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. - *
- */ - -package io.shardingsphere.transaction.common.event; - -import io.shardingsphere.core.constant.TCLType; -import io.shardingsphere.transaction.common.TransactionContextHolder; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * Transaction event factory. - * - * @author zhaojun - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TransactionEventFactory { - - /** - * Create transaction event. - * - * @param tclType TCL type - * @return transaction event - */ - public static TransactionEvent create(final TCLType tclType) { - switch (TransactionContextHolder.get().getTransactionType()) { - case LOCAL: - return new LocalTransactionEvent(tclType); - case XA: - return new XaTransactionEvent(tclType, ""); - case BASE: - default: - return null; - } - } -} diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/XaTransactionEvent.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/XaTransactionEvent.java deleted file mode 100644 index 67c9dd5ecd15c..0000000000000 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/XaTransactionEvent.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * 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. - *
- */ - -package io.shardingsphere.transaction.common.event; - -import com.google.common.base.Optional; -import io.shardingsphere.core.constant.TCLType; -import io.shardingsphere.core.exception.ShardingException; -import lombok.Getter; - -/** - * XA transaction event. - * - * @author zhaojun - */ -@Getter -public final class XaTransactionEvent extends TransactionEvent { - - private final String sql; - - public XaTransactionEvent(final TCLType tclType, final String sql) { - super(tclType); - this.sql = sql; - } - - @Override - public Optional- * 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. - *
- */ - -package io.shardingsphere.transaction.common.listener; - -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.common.event.TransactionEvent; - -import java.sql.SQLException; - -/** - * Transaction Listener. - * - * @author zhaojun - */ -public final class TransactionListener { - - /** - * Register transaction listener into event bus. - */ - public void register() { - EventBusInstance.getInstance().register(this); - } - - /** - * Listen event. - * - * @param transactionEvent transaction event - * @throws SQLException SQL exception - */ - @Subscribe - @AllowConcurrentEvents - public void listen(final TransactionEvent transactionEvent) throws SQLException { - ShardingTransactionManager transactionManager = TransactionContextHolder.get().getTransactionManager(); - switch (transactionEvent.getTclType()) { - case BEGIN: - transactionManager.begin(transactionEvent); - break; - case COMMIT: - transactionManager.commit(transactionEvent); - break; - case ROLLBACK: - transactionManager.rollback(transactionEvent); - break; - default: - } - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/constant/TCLType.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/event/ShardingTransactionEvent.java similarity index 63% rename from sharding-core/src/main/java/io/shardingsphere/core/constant/TCLType.java rename to sharding-transaction/src/main/java/io/shardingsphere/transaction/event/ShardingTransactionEvent.java index 5f0bdbba2b8db..725578a81604d 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/constant/TCLType.java +++ b/sharding-transaction/src/main/java/io/shardingsphere/transaction/event/ShardingTransactionEvent.java @@ -15,14 +15,22 @@ * */ -package io.shardingsphere.core.constant; +package io.shardingsphere.transaction.event; + +import io.shardingsphere.core.constant.transaction.TransactionOperationType; /** - * TCL Type. + * Sharding transaction event. * * @author zhaojun + * @author zhangliang */ -public enum TCLType { +public interface ShardingTransactionEvent { - SET, BEGIN, COMMIT, ROLLBACK, SAVEPOINT + /** + * Get transaction operation type. + * + * @return transaction operation type + */ + TransactionOperationType getOperationType(); } diff --git a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/LocalTransactionEvent.java b/sharding-transaction/src/main/java/io/shardingsphere/transaction/event/local/LocalTransactionEvent.java similarity index 54% rename from sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/LocalTransactionEvent.java rename to sharding-transaction/src/main/java/io/shardingsphere/transaction/event/local/LocalTransactionEvent.java index 63cce2ab9390a..e225eaff8db8f 100644 --- a/sharding-transaction/src/main/java/io/shardingsphere/transaction/common/event/LocalTransactionEvent.java +++ b/sharding-transaction/src/main/java/io/shardingsphere/transaction/event/local/LocalTransactionEvent.java @@ -15,13 +15,12 @@ * */ -package io.shardingsphere.transaction.common.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.core.constant.transaction.TransactionOperationType; +import io.shardingsphere.transaction.event.ShardingTransactionEvent; import lombok.Getter; -import lombok.Setter; +import lombok.RequiredArgsConstructor; import java.sql.Connection; import java.util.Collection; @@ -31,20 +30,13 @@ * * @author zhaojun */ +@RequiredArgsConstructor @Getter -@Setter -public final class LocalTransactionEvent extends TransactionEvent { +public final class LocalTransactionEvent implements ShardingTransactionEvent { - private Collection+ * 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. + *
+ */ + +package io.shardingsphere.transaction.listener; + +import io.shardingsphere.core.util.EventBusInstance; +import io.shardingsphere.transaction.event.ShardingTransactionEvent; +import io.shardingsphere.transaction.manager.ShardingTransactionManager; + +import java.sql.SQLException; + +/** + * Sharding transaction listener adapter. + * + * @author zhangliang + * + * @param+ * 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. + *
+ */ + +package io.shardingsphere.transaction.listener.local; + +import com.google.common.eventbus.AllowConcurrentEvents; +import com.google.common.eventbus.Subscribe; +import io.shardingsphere.core.constant.transaction.TransactionType; +import io.shardingsphere.transaction.event.local.LocalTransactionEvent; +import io.shardingsphere.transaction.listener.ShardingTransactionListenerAdapter; +import io.shardingsphere.transaction.manager.ShardingTransactionManager; +import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry; + +import java.sql.SQLException; + +/** + * Local transaction listener. + * + * @author zhangliang + */ +public final class LocalTransactionListener extends ShardingTransactionListenerAdapter+ * 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. + *
+ */ + +package io.shardingsphere.transaction.listener.xa; + +import com.google.common.eventbus.AllowConcurrentEvents; +import com.google.common.eventbus.Subscribe; +import io.shardingsphere.core.constant.transaction.TransactionType; +import io.shardingsphere.transaction.event.xa.XATransactionEvent; +import io.shardingsphere.transaction.listener.ShardingTransactionListenerAdapter; +import io.shardingsphere.transaction.manager.ShardingTransactionManager; +import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry; + +import java.sql.SQLException; + +/** + * XA transaction listener. + * + * @author zhangliang + */ +public final class XATransactionListener extends ShardingTransactionListenerAdapter+ * 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. + *
+ */ + +package io.shardingsphere.transaction.manager; + +import io.shardingsphere.core.constant.transaction.TransactionType; +import io.shardingsphere.transaction.manager.local.LocalTransactionManager; +import io.shardingsphere.transaction.manager.xa.XATransactionManagerSPILoader; + +import java.util.HashMap; +import java.util.Map; + +/** + * Sharding transaction manager register. + * + * @author zhangliang + */ +public final class ShardingTransactionManagerRegistry { + + private static final ShardingTransactionManagerRegistry INSTANCE = new ShardingTransactionManagerRegistry(); + + private final Map+ * 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. + *
+ */ + +package io.shardingsphere.transaction.manager.xa; + +import io.shardingsphere.transaction.event.xa.XATransactionEvent; +import io.shardingsphere.core.rule.DataSourceParameter; +import io.shardingsphere.transaction.manager.ShardingTransactionManager; + +import javax.sql.DataSource; +import javax.sql.XADataSource; + +/** + * XA transaction manager. + * + * @author zhangliang + */ +public interface XATransactionManager extends ShardingTransactionManager+ * 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. + *
+ */ + +package io.shardingsphere.transaction.manager.xa.atomikos; + +import com.atomikos.icatch.jta.UserTransactionManager; +import com.atomikos.jdbc.AtomikosDataSourceBean; +import com.google.common.base.Optional; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.transaction.event.xa.XATransactionEvent; +import io.shardingsphere.core.rule.DataSourceParameter; +import io.shardingsphere.transaction.manager.xa.XATransactionManager; + +import javax.sql.DataSource; +import javax.sql.XADataSource; +import javax.transaction.HeuristicMixedException; +import javax.transaction.HeuristicRollbackException; +import javax.transaction.NotSupportedException; +import javax.transaction.RollbackException; +import javax.transaction.SystemException; +import java.sql.SQLException; +import java.util.Properties; + +/** + * Atomikos XA transaction manager. + * + * @author zhaojun + */ +public final class AtomikosTransactionManager implements XATransactionManager { + + private static final UserTransactionManager USER_TRANSACTION_MANAGER = new UserTransactionManager(); + + static { + try { + USER_TRANSACTION_MANAGER.init(); + } catch (final SystemException ex) { + throw new ShardingException(ex); + } + } + + @Override + public void begin(final XATransactionEvent event) throws SQLException { + try { + USER_TRANSACTION_MANAGER.begin(); + } catch (final SystemException | NotSupportedException ex) { + throw new SQLException(ex); + } + } + + @Override + public void commit(final XATransactionEvent event) throws SQLException { + try { + USER_TRANSACTION_MANAGER.commit(); + } catch (final RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException ex) { + throw new SQLException(ex); + } + } + + @Override + public void rollback(final XATransactionEvent event) throws SQLException { + try { + USER_TRANSACTION_MANAGER.rollback(); + } catch (final SystemException ex) { + throw new SQLException(ex); + } + } + + @Override + public int getStatus() throws SQLException { + try { + return USER_TRANSACTION_MANAGER.getStatus(); + } catch (final SystemException ex) { + throw new SQLException(ex); + } + } + + @Override + public DataSource wrapDataSource(final XADataSource dataSource, final String dataSourceName, final DataSourceParameter dataSourceParameter) { + AtomikosDataSourceBean result = new AtomikosDataSourceBean(); + result.setUniqueResourceName(dataSourceName); + result.setMaxPoolSize(dataSourceParameter.getMaximumPoolSize()); + result.setTestQuery("SELECT 1"); + result.setXaProperties(getProperties(dataSourceParameter)); + result.setXaDataSource(dataSource); + return result; + } + + private Properties getProperties(final DataSourceParameter dataSourceParameter) { + Properties result = new Properties(); + result.setProperty("user", dataSourceParameter.getUsername()); + result.setProperty("password", Optional.fromNullable(dataSourceParameter.getPassword()).or("")); + result.setProperty("URL", dataSourceParameter.getUrl()); + result.setProperty("pinGlobalTxToPhysicalConnection", Boolean.TRUE.toString()); + result.setProperty("autoReconnect", Boolean.TRUE.toString()); + result.setProperty("useServerPrepStmts", Boolean.TRUE.toString()); + result.setProperty("cachePrepStmts", Boolean.TRUE.toString()); + result.setProperty("prepStmtCacheSize", "250"); + result.setProperty("prepStmtCacheSqlLimit", "2048"); + result.setProperty("useLocalSessionState", Boolean.TRUE.toString()); + result.setProperty("rewriteBatchedStatements", Boolean.TRUE.toString()); + result.setProperty("cacheResultSetMetadata", Boolean.TRUE.toString()); + result.setProperty("cacheServerConfiguration", Boolean.TRUE.toString()); + result.setProperty("elideSetAutoCommits", Boolean.TRUE.toString()); + result.setProperty("maintainTimeStats", Boolean.FALSE.toString()); + result.setProperty("netTimeoutForStreamingResults", "0"); + return result; + } +}