Skip to content

Commit

Permalink
Merge pull request #1217 from cherrylzhao/dev-new
Browse files Browse the repository at this point in the history
Move local transaction to sharding-jdbc
  • Loading branch information
terrymanu authored Sep 1, 2018
2 parents 3e258ac + b0a5139 commit 2723972
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
* </p>
*/

package io.shardingsphere.transaction.listener.local;
package io.shardingsphere.core.transaction;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.constant.transaction.TransactionType;
import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.core.event.transaction.local.LocalTransactionEvent;
import io.shardingsphere.transaction.listener.ShardingTransactionListenerAdapter;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;
import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry;

import java.sql.SQLException;

Expand All @@ -32,14 +29,30 @@
*
* @author zhangliang
*/
public final class LocalTransactionListener extends ShardingTransactionListenerAdapter<LocalTransactionEvent> {
public final class LocalTransactionListener implements ShardingTransactionListener<LocalTransactionEvent> {

private final ShardingTransactionManager shardingTransactionManager = ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionType.LOCAL);
private final ShardingTransactionManager shardingTransactionManager = new LocalTransactionManager();

@Override
public void register() {
ShardingEventBusInstance.getInstance().register(this);
}

@Subscribe
@AllowConcurrentEvents
@Override
public void listen(final LocalTransactionEvent transactionEvent) throws SQLException {
doTransaction(shardingTransactionManager, transactionEvent);
switch (transactionEvent.getOperationType()) {
case BEGIN:
shardingTransactionManager.begin(transactionEvent);
break;
case COMMIT:
shardingTransactionManager.commit(transactionEvent);
break;
case ROLLBACK:
shardingTransactionManager.rollback(transactionEvent);
break;
default:
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

package io.shardingsphere.core.transaction;

import io.shardingsphere.core.event.ShardingEventListenerRegistry;

/**
* Transaction listener registry.
*
* @author zhangliang
*/
public final class LocalTransactionListenerRegistry implements ShardingEventListenerRegistry {

@Override
public void register() {
new LocalTransactionListener().register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
* </p>
*/

package io.shardingsphere.transaction.manager.local;
package io.shardingsphere.core.transaction;

import io.shardingsphere.core.event.transaction.local.LocalTransactionEvent;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;

import javax.transaction.Status;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
Expand Down Expand Up @@ -82,10 +80,4 @@ private void throwSQLExceptionIfNecessary(final Collection<SQLException> excepti
}
throw sqlException;
}

@Override
public int getStatus() {
// TODO :zhaojun need confirm, return Status.STATUS_NO_TRANSACTION or zero?
return Status.STATUS_NO_TRANSACTION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

package io.shardingsphere.core.transaction;

import io.shardingsphere.core.event.transaction.ShardingTransactionEvent;

import java.sql.SQLException;

/**
* Sharding transaction listener.
*
* @author zhangliang
*
* @param <T> transaction event type
*/
// TODO maybe move this listener to sharding-core is better.
public interface ShardingTransactionListener<T extends ShardingTransactionEvent> {

/**
* Register sharding transaction listener into event bus.
*/
void register();

/**
* Listen event.
*
* @param transactionEvent transaction event
* @throws SQLException SQL exception
*/
void listen(T transactionEvent) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

package io.shardingsphere.core.transaction;

import io.shardingsphere.core.event.transaction.ShardingTransactionEvent;

import java.sql.SQLException;

/**
* Sharding transaction manager.
*
* @author zhaojun
* @author zhangliang
*
* @param <T> transaction event type
*/
public interface ShardingTransactionManager<T extends ShardingTransactionEvent> {

/**
* Begin transaction.
*
* @param transactionEvent transaction event
* @throws SQLException SQL exception
*/
void begin(T transactionEvent) throws SQLException;

/**
* Commit transaction.
*
* @param transactionEvent transaction event
* @throws SQLException SQL exception
*/
void commit(T transactionEvent) throws SQLException;

/**
* Rollback transaction.
*
* @param transactionEvent transaction event
* @throws SQLException SQL exception
*/
void rollback(T transactionEvent) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.shardingsphere.core.transaction.LocalTransactionListenerRegistry
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
* </p>
*/

package io.shardingsphere.transaction.listener.local;
package io.shardingsphere.core.transaction;

import com.google.common.eventbus.EventBus;
import io.shardingsphere.core.constant.transaction.TransactionOperationType;
import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.core.event.transaction.local.LocalTransactionEvent;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingsphere.transaction.manager.local;
package io.shardingsphere.core.transaction;

import io.shardingsphere.core.constant.transaction.TransactionOperationType;
import io.shardingsphere.core.event.transaction.local.LocalTransactionEvent;
Expand All @@ -24,13 +24,10 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import javax.transaction.Status;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -96,9 +93,4 @@ public void assertRollbackWithException() throws SQLException {
verify(connection2).rollback();
}
}

@Test
public void assertGetStatus() {
assertThat(new LocalTransactionManager().getStatus(), is(Status.STATUS_NO_TRANSACTION));
}
}
3 changes: 0 additions & 3 deletions sharding-proxy/src/main/resources/conf/jta.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package io.shardingsphere.transaction;

import io.shardingsphere.core.event.ShardingEventListenerRegistry;
import io.shardingsphere.transaction.listener.local.LocalTransactionListener;
import io.shardingsphere.transaction.listener.xa.XATransactionListener;

/**
Expand All @@ -30,7 +29,6 @@ public final class TransactionListenerRegistry implements ShardingEventListenerR

@Override
public void register() {
new LocalTransactionListener().register();
new XATransactionListener().register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;
Expand All @@ -43,8 +42,6 @@ private ShardingTransactionManagerRegistry() {

private ShardingTransactionManager loadShardingTransactionManager(final TransactionType transactionType) {
switch (transactionType) {
case LOCAL:
return new LocalTransactionManager();
case XA:
return XATransactionManagerSPILoader.getInstance().getTransactionManager();
case BASE:
Expand Down
4 changes: 4 additions & 0 deletions sharding-transaction/src/main/resources/jta.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
com.atomikos.icatch.serial_jta_transactions = false
com.atomikos.icatch.default_jta_timeout = 1000000
com.atomikos.icatch.max_actives = 10000
com.atomikos.icatch.enable_logging = false
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

package io.shardingsphere.transaction.listener;

import io.shardingsphere.transaction.listener.local.LocalTransactionListenerTest;
import io.shardingsphere.transaction.listener.xa.XATransactionListenerTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({
LocalTransactionListenerTest.class,
XATransactionListenerTest.class
})
public final class AllListenerTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package io.shardingsphere.transaction.manager;

import io.shardingsphere.transaction.manager.base.SagaTransactionManagerTest;
import io.shardingsphere.transaction.manager.local.LocalTransactionManagerTest;
import io.shardingsphere.transaction.manager.xa.XATransactionManagerSPILoaderTest;
import io.shardingsphere.transaction.manager.xa.atomikos.AtomikosTransactionManagerTest;
import org.junit.runner.RunWith;
Expand All @@ -28,8 +27,7 @@
@RunWith(Suite.class)
@SuiteClasses({
ShardingTransactionManagerRegistryTest.class,
LocalTransactionManagerTest.class,
XATransactionManagerSPILoaderTest.class,
XATransactionManagerSPILoaderTest.class,
AtomikosTransactionManagerTest.class,
SagaTransactionManagerTest.class
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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.XATransactionManager;
import org.junit.Test;

Expand All @@ -27,11 +26,6 @@

public final class ShardingTransactionManagerRegistryTest {

@Test
public void assertGetShardingTransactionManagerForLocal() {
assertThat(ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionType.LOCAL), instanceOf(LocalTransactionManager.class));
}

@Test
public void assertGetShardingTransactionManagerForXA() {
assertThat(ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionType.XA), instanceOf(XATransactionManager.class));
Expand Down

0 comments on commit 2723972

Please sign in to comment.