Created
August 14, 2016 13:44
-
-
Save zhugw/1cdadc73358088a0989b5c3f36a92cac to your computer and use it in GitHub Desktop.
验证Spring嵌套事务
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.helijia.lottery.service; | |
import com.helijia.common.api.model.ApiException; | |
import com.helijia.lottery.dao.mapper.TestMapper; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.TransactionDefinition; | |
import org.springframework.transaction.TransactionStatus; | |
import org.springframework.transaction.annotation.Transactional; | |
import org.springframework.transaction.support.DefaultTransactionDefinition; | |
import org.springframework.transaction.support.TransactionCallbackWithoutResult; | |
import org.springframework.transaction.support.TransactionTemplate; | |
/** | |
* Created by duanmu on 8/11/16. | |
*/ | |
@Service | |
public class FooService { | |
@Autowired | |
private TestMapper mapper; | |
@Autowired | |
private BarService barService; | |
@Autowired | |
private DataSourceTransactionManager txManager; | |
@Autowired | |
private TransactionTemplate transactionTemplate; | |
@Transactional | |
public void foo() { | |
doSomething(); | |
callBar(); | |
// callBar2(); | |
// callBar3(); | |
doSomethingElse(); | |
} | |
@Transactional | |
public void foo2() { | |
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED); | |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { | |
@Override | |
protected void doInTransactionWithoutResult(TransactionStatus status) { | |
doSomething(); | |
} | |
}); | |
callBar4(); | |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { | |
@Override | |
protected void doInTransactionWithoutResult(TransactionStatus status) { | |
doSomethingElse(); | |
} | |
}); | |
} | |
private void callBar() { | |
try { | |
barService.bar(); | |
} catch (ApiException e) { | |
System.out.println("call bar failed"); | |
e.printStackTrace(); | |
} | |
} | |
private void callBar2() { | |
DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); | |
TransactionStatus status = txManager.getTransaction(def); | |
try { | |
barService.bar(); | |
txManager.commit(status); | |
} catch (ApiException e) { | |
System.out.println("call bar failed"); | |
e.printStackTrace(); | |
txManager.rollback(status); | |
} | |
} | |
private void callBar3() { | |
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); | |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { | |
protected void doInTransactionWithoutResult(TransactionStatus status) { | |
try { | |
barService.bar(); | |
} catch (ApiException e) { | |
System.out.println("call bar3 failed"); | |
e.printStackTrace(); | |
status.setRollbackOnly(); | |
} | |
} | |
}); | |
} | |
private void callBar4() { | |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { | |
protected void doInTransactionWithoutResult(TransactionStatus status) { | |
Object savepoint = status.createSavepoint(); | |
try { | |
barService.bar(); | |
} catch (ApiException e) { | |
System.out.println("call bar3 failed"); | |
e.printStackTrace(); | |
status.rollbackToSavepoint(savepoint); | |
} | |
} | |
}); | |
} | |
private void doSomethingElse() { | |
System.out.println("doSomethingElse"); | |
mapper.getNow(); | |
mapper.insertFoo(RandomStringUtils.randomAlphanumeric(10)); | |
} | |
private void doSomething() { | |
System.out.println("doSomething"); | |
mapper.getVersion(); | |
mapper.insertFoo(RandomStringUtils.randomAlphanumeric(10)); | |
} | |
} | |
package com.helijia.lottery.service; | |
import com.helijia.common.api.model.ApiException; | |
import com.helijia.lottery.dao.mapper.TestMapper; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
/** | |
* Created by duanmu on 8/11/16. | |
*/ | |
@Service | |
public class BarService { | |
@Autowired | |
private TestMapper mapper; | |
@Transactional | |
public void bar() { | |
System.out.println("in bar"); | |
mapper.getBar(); | |
if (1 == 1) { | |
throw new ApiException("100000", "领券失败"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment