Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resovle fence to rm #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tcc/dubbo-tcc-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version.2.x}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface TccActionOne {
* @param a the a
* @return the boolean
*/
@TwoPhaseBusinessAction(name = "DubboTccActionOne", commitMethod = "commit", rollbackMethod = "rollback")
@TwoPhaseBusinessAction(name = "DubboTccActionOne", commitMethod = "commit", rollbackMethod = "rollback", useTCCFence = true)
public boolean prepare(BusinessActionContext actionContext, @BusinessActionContextParameter(paramName = "a") int a);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface TccActionTwo {
* @param list the list
* @return the boolean
*/
@TwoPhaseBusinessAction(name = "DubboTccActionTwo", commitMethod = "commit", rollbackMethod = "rollback")
@TwoPhaseBusinessAction(name = "DubboTccActionTwo", commitMethod = "commit", rollbackMethod = "rollback", useTCCFence = true)
public boolean prepare(BusinessActionContext actionContext,
@BusinessActionContextParameter(paramName = "b") String b,
@BusinessActionContextParameter(paramName = "c", index = 1) List list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) throws Exception {
@Override
protected void start0(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] {"spring/seata-tcc.xml", "spring/seata-dubbo-reference.xml"});
new String[] {"spring/seata-tcc-reference.xml", "spring/seata-dubbo-reference.xml"});
tccTransactionService = (TccTransactionService)applicationContext.getBean("tccTransactionService");

//分布式事务提交demo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package io.seata.samples.tcc.dubbo.starter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import io.seata.samples.jit.AbstractStarter;
import io.seata.samples.jit.ApplicationKeeper;
import org.apache.curator.test.TestingServer;
Expand All @@ -40,10 +45,34 @@ protected void start0(String[] args) throws Exception {
mockZKServer();

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] {"spring/seata-tcc.xml", "spring/seata-dubbo-provider.xml"});
new String[] {"spring/seata-tcc-provider.xml", "spring/seata-dubbo-provider.xml", "db/provider-commom-fence-datasource.xml"});

// create fence log table
DataSource dataSource = (DataSource) applicationContext.getBean("seataTCCFenceDataSource");
this.createFenceLogTableSql(dataSource);

new ApplicationKeeper().keep();
}

private void createFenceLogTableSql(DataSource dataSource) throws SQLException {
Connection connection = dataSource.getConnection();
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `tcc_fence_log`\n" +
"(\n" +
" `xid` VARCHAR(128) NOT NULL COMMENT 'global id',\n" +
" `branch_id` BIGINT NOT NULL COMMENT 'branch id',\n" +
" `action_name` VARCHAR(64) NOT NULL COMMENT 'action name',\n" +
" `status` TINYINT NOT NULL COMMENT 'status(tried:1;committed:2;rollbacked:3;suspended:4)',\n" +
" `gmt_create` DATETIME(3) NOT NULL COMMENT 'create time',\n" +
" `gmt_modified` DATETIME(3) NOT NULL COMMENT 'update time',\n" +
" PRIMARY KEY (`xid`, `branch_id`),\n" +
" KEY `idx_gmt_modified` (`gmt_modified`),\n" +
" KEY `idx_status` (`status`)\n" +
") ;");
stmt.close();
connection.close();
}

private static void mockZKServer() throws Exception {
//Mock zk server,作为 transfer 配置中心
server = new TestingServer(2181, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~
~ 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.
-->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">

<bean id="seataTCCFenceDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.h2.Driver</value>
</property>
<property name="url">
<value>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;</value>
</property>
<property name="username">
<value>fence</value>
</property>
<property name="password">
<value></value>
</property>
</bean>

<bean id="seataTCCFenceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="seataTCCFenceDataSource"/>
</bean>

<bean class="io.seata.rm.fence.SpringFenceConfig">
<constructor-arg ref="seataTCCFenceDataSource"/>
<constructor-arg ref="seataTCCFenceTransactionManager"/>
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@

<bean id="tccActionTwoImpl" class="io.seata.samples.tcc.dubbo.action.impl.TccActionTwoImpl"/>

<bean id="tccTransactionService" class="io.seata.samples.tcc.dubbo.service.TccTransactionService"/>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~
~ 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.
-->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">

<!-- fescar bean scanner -->
<bean class="io.seata.spring.annotation.GlobalTransactionScanner">
<constructor-arg value="tcc-sample"/>
<constructor-arg value="my_test_tx_group"/>
</bean>

<bean id="tccTransactionService" class="io.seata.samples.tcc.dubbo.service.TccTransactionService"/>

</beans>
10 changes: 10 additions & 0 deletions tcc/local-tcc-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version.2.x}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface TccActionOne {
* @param a the a
* @return the boolean
*/
@TwoPhaseBusinessAction(name = "TccActionOne", commitMethod = "commit", rollbackMethod = "rollback")
@TwoPhaseBusinessAction(name = "TccActionOne", commitMethod = "commit", rollbackMethod = "rollback", useTCCFence = true)
public boolean prepare(BusinessActionContext actionContext, int a);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface TccActionTwo {
* @param b the b
* @return the boolean
*/
@TwoPhaseBusinessAction(name = "TccActionTwo", commitMethod = "commit", rollbackMethod = "rollback")
@TwoPhaseBusinessAction(name = "TccActionTwo", commitMethod = "commit", rollbackMethod = "rollback", useTCCFence = true)
public boolean prepare(BusinessActionContext actionContext, String b);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package io.seata.samples.tcc.starter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -61,14 +65,18 @@ public class LocalTccTransactionStarter {
* @param args the input arguments
* @throws InterruptedException the interrupted exception
*/
public static void main(String[] args) throws InterruptedException {
applicationContext = new ClassPathXmlApplicationContext(new String[] {"spring/seata-tcc.xml"});
public static void main(String[] args) throws InterruptedException, SQLException {
applicationContext = new ClassPathXmlApplicationContext(new String[] {"spring/seata-tcc.xml","db/provider-commom-fence-datasource.xml"});

tccTransactionService = (TccTransactionService)applicationContext.getBean("tccTransactionService");

tccActionOne = (TccActionOneImpl)applicationContext.getBean("tccActionOne");
tccActionTwo = (TccActionTwoImpl)applicationContext.getBean("tccActionTwo");

// create fence log table
DataSource dataSource = (DataSource) applicationContext.getBean("seataTCCFenceDataSource");
createFenceLogTableSql(dataSource);

//分布式事务提交demo
transactionCommitDemo();

Expand Down Expand Up @@ -108,4 +116,23 @@ private static void transactionRollbackDemo() throws InterruptedException {
System.out.println("transaction rollback demo finish.");
}

private static void createFenceLogTableSql(DataSource dataSource) throws SQLException {
Connection connection = dataSource.getConnection();
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `tcc_fence_log`\n" +
"(\n" +
" `xid` VARCHAR(128) NOT NULL COMMENT 'global id',\n" +
" `branch_id` BIGINT NOT NULL COMMENT 'branch id',\n" +
" `action_name` VARCHAR(64) NOT NULL COMMENT 'action name',\n" +
" `status` TINYINT NOT NULL COMMENT 'status(tried:1;committed:2;rollbacked:3;suspended:4)',\n" +
" `gmt_create` DATETIME(3) NOT NULL COMMENT 'create time',\n" +
" `gmt_modified` DATETIME(3) NOT NULL COMMENT 'update time',\n" +
" PRIMARY KEY (`xid`, `branch_id`),\n" +
" KEY `idx_gmt_modified` (`gmt_modified`),\n" +
" KEY `idx_status` (`status`)\n" +
") ;");
stmt.close();
connection.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~
~ 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.
-->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">

<bean id="seataTCCFenceDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.h2.Driver</value>
</property>
<property name="url">
<value>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;</value>
</property>
<property name="username">
<value>fence</value>
</property>
<property name="password">
<value></value>
</property>
</bean>

<bean id="seataTCCFenceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="seataTCCFenceDataSource"/>
</bean>

<bean class="io.seata.rm.fence.SpringFenceConfig">
<constructor-arg ref="seataTCCFenceDataSource"/>
<constructor-arg ref="seataTCCFenceTransactionManager"/>
</bean>

</beans>
11 changes: 11 additions & 0 deletions tcc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<module>hsf-tcc-sample</module>
</modules>

<properties>
<seata.version.2.x>2.0.0-SNAPSHOT</seata.version.2.x>
<commons-dbcp.version>1.2.2</commons-dbcp.version>
</properties>

<dependencyManagement>
<dependencies>
<!-- dubbo -->
Expand All @@ -44,6 +49,12 @@
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down