Spring Transaction Management in Multi-Thread? - spring

Spring Transaction doesn't support multi-thread, so I try to manage transaction manually in Thread's run() method. But, It doesn't work!
I'd like to rollback each thread's run() method in below example when there's a exception throw inside it. (In below case, INSERT INTO UNKNOWN_TABLE)
My expected result is 'start, 1, 3, 5, end'.
And the actual result is 'start, 1, 2, 3, 4, 5, end'.
Any reply is welcome! Thanks!
Main Class:
#SpringBootApplication
public class Application implements CommandLineRunner {
#Autowired
private TestService testService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public DriverManagerDataSource createDriverManagerDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setJdbcUrl("jdbc:oracle:thin:#url:port/schema");
dataSource.setUsername("xxxx");
dataSource.setPassword("xxxx");
return dataSource;
}
#Bean
public JdbcTemplate createJdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(createDriverManagerDataSource());
return jdbcTemplate;
}
#Override
public void run(String... args) throws Exception {
testService.test();
}
}
Service Class:
#Service
public class TestService {
#Autowired
private JdbcTemplate jdbcTemplate;
#Transactional(rollbackFor = Exception.class)
public void test() throws Exception {
jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('start', 'start')");
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 5; i++) {
executorService.submit(new TestRunner(i));
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('end', 'end')");
}
private class TestRunner implements Runnable {
private Integer id;
public TestRunner(Integer id) {
this.id = id;
}
#Override
public void run() {
try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
try {
connection.setAutoCommit(false);
String sqlString = String.format("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('%d', '%d')", id, id);
jdbcTemplate.batchUpdate(sqlString);
if (id % 2 == 0) {
// Except the transaction been rollback when this.id is 2 or 4.
jdbcTemplate.batchUpdate("INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES ('no', 'no')");
}
connection.commit();
} catch (Exception e) {
System.err.println("Failure: UNKNOWN_TABLE");
connection.rollback();
} finally {
connection.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
}

A couple of things with your code as you are trying to outsmart both Spring and Spring Boot. Instead of trying to do that, work with the frameworks instead of around them.
Ditch your #Configuration class and let Spring Boot do the configuration
Use TransactionTemplate instead of messing around with the (wrong!) Connection yourself.
Use the Spring TaskExecutor, configured by default, instead of manual access to a Executor.
Add this to your application.properties
spring.datasource.url=jdbc:oracle:thin:#url:port/schema
spring.datasource.username=xxxx
spring.datasource.password=xxxx
Instead of the messing around with the connection use a TransactionTemplate.
##SpringBootApplication
public class Application {
private static final String SQL = "INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES (?, ?)";
private static final String ERROR_SQL = "INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES (?, ?)";
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner testRunner(JdbcTemplate jdbc, TransactionTemplate tx, TaskExecutor tasks) {
return (args) -> {
jdbc.update(SQL, "start", "start");
IntStream.range(1, 6)
.forEach(id -> {
try {
tasks.execute(() -> tx.executeWithoutResult((s) -> {
jdbc.update(SQL, id, id);
if (id % 2 == 0) {
jdbc.update(ERROR_SQL, "no", "no");
}
}));
} catch (DataAccessException e) {
e.printStackTrace();
}
});
jdbc.update(SQL, "end", "end");
};
}
}
Something like the above would yield the result you want. Notice that you now use the JdbcTemplate, TransactionTemplate and TaskExecutor as provided by the frameworks.

After refer to #M. Deinum answer, I have change my code to below and it meet's my needs.
application.properties
spring.datasource.url=jdbc:oracle:thin:#ip:port/schema
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
Main Class
#SpringBootApplication
public class Application implements CommandLineRunner {
#Autowired
private TestService testService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String... args) throws Exception {
testService.test();
System.exit(0);
}
}
TestService
#Service
public class TestService {
#Autowired
private JdbcTemplate jdbcTemplate;
#Autowired
private PlatformTransactionManager transactionManager;
#Transactional(rollbackFor = Exception.class)
public void test() throws Exception {
jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('start', 'start')");
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 5; i++) {
executorService.submit(new TestRunner(i));
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('end', 'end')");
}
private class TestRunner implements Runnable {
private Integer id;
public TestRunner(Integer id) {
this.id = id;
}
#Override
public void run() {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
#Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
String sqlString = String.format("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('%d', '%d')", id, id);
jdbcTemplate.batchUpdate(sqlString);
if (id % 2 == 0) {
jdbcTemplate.batchUpdate("INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES ('no', 'no')");
}
}
});
}
}
}
With result 'start, 1, 3, 5, end'.

Related

Spring Batch Reader is reading alternate records

I have created a sample spring batch application which is trying to read record from a DB and in writer, it displays those records. However, I could see that only even numbered (alternate) records are printed.
It's not the problem of database as the behavior is consistent with both H2 database or Oracle database.
There are total 100 records in my DB.
With JDBCCursorItemReader, only 50 records are read and that too alternate one as can be seen from log snapshot
With JdbcPagingItemReader, only 5 records are read and that too alternate one as can be seen from log snapshot
My code configurations are given below. Why reader is skipping odd numbered records?
#Bean
public ItemWriter<Safety> safetyWriter() {
return items -> {
for (Safety item : items) {
log.info(item.toString());
}
};
}
#Bean
public JdbcCursorItemReader<Safety> cursorItemReader() throws Exception {
JdbcCursorItemReader<Safety> reader = new JdbcCursorItemReader<>();
reader.setSql("select * from safety " );
reader.setDataSource(dataSource);
reader.setRowMapper(new SafetyRowMapper());
reader.setVerifyCursorPosition(false);
reader.afterPropertiesSet();
return reader;
}
#Bean
JdbcPagingItemReader<Safety> safetyPagingItemReader() throws Exception {
JdbcPagingItemReader<Safety> reader = new JdbcPagingItemReader<>();
reader.setDataSource(dataSource);
reader.setFetchSize(10);
reader.setRowMapper(new SafetyRowMapper());
H2PagingQueryProvider queryProvider = new H2PagingQueryProvider();
queryProvider.setSelectClause("*");
queryProvider.setFromClause("safety");
Map<String, Order> sortKeys = new HashMap<>(1);
sortKeys.put("id", Order.ASCENDING);
queryProvider.setSortKeys(sortKeys);
reader.setQueryProvider(queryProvider);
return reader;
}
#Bean
public Step importSafetyDetails() throws Exception {
return stepBuilderFactory.get("importSafetyDetails")
.<Safety, Safety>chunk(chunkSize)
//.reader(cursorItemReader())
.reader(safetyPagingItemReader())
.writer(safetyWriter())
.listener(new StepListener())
.listener(new ChunkListener())
.build();
}
#Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.start(importSafetyDetails())
.build();
}
Domain classes looks like below:
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Safety {
private int id;
}
public class SafetyRowMapper implements RowMapper<Safety> {
#Override
public Safety mapRow(ResultSet resultSet, int i) throws SQLException {
if(resultSet.next()) {
Safety safety = new Safety();
safety.setId(resultSet.getInt("id"));
return safety;
}
return null;
}
}
#SpringBootApplication
#EnableBatchProcessing
public class SpringBatchSamplesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchSamplesApplication.class, args);
}
}
application.yml configuration is as below:
spring:
application:
name: spring-batch-samples
main:
allow-bean-definition-overriding: true
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driver-class-name: org.h2.Driver
hikari:
connection-timeout: 20000
maximum-pool-size: 10
h2:
console:
enabled: true
batch:
initialize-schema: never
server:
port: 9090
sqls are as below:
CREATE TABLE safety (
id int NOT NULL,
CONSTRAINT PK_ID PRIMARY KEY (id)
);
INSERT INTO safety (id) VALUES (1);
...100 records are inserted
Listeners classes are as below:
#Slf4j
public class StepListener{
#AfterStep
public ExitStatus afterStep(StepExecution stepExecution) {
log.info("In step {} ,Exit Status: {} ,Read Records: {} ,Committed Records: {} ,Skipped Read Records: {} ,Skipped Write Records: {}",
stepExecution.getStepName(),
stepExecution.getExitStatus().getExitCode(),
stepExecution.getReadCount(),
stepExecution.getCommitCount(),
stepExecution.getReadSkipCount(),
stepExecution.getWriteSkipCount());
return stepExecution.getExitStatus();
}
}
#Slf4j
public class ChunkListener {
#BeforeChunk
public void beforeChunk(ChunkContext context) {
log.info("<< Before the chunk");
}
#AfterChunk
public void afterChunk(ChunkContext context) {
log.info("<< After the chunk");
}
}
I tried to reproduce your problem, but I couldn't. Maybe it would be great if you could share more code.
Meanwhile I created a simple job to read 100 records from "safety" table a print them to the console. And it is working fine.
.
#SpringBootApplication
#EnableBatchProcessing
public class ReaderWriterProblem implements CommandLineRunner {
#Autowired
DataSource dataSource;
#Autowired
StepBuilderFactory stepBuilderFactory;
#Autowired
JobBuilderFactory jobBuilderFactory;
#Autowired
private JobLauncher jobLauncher;
#Autowired
private ApplicationContext context;
public static void main(String[] args) {
String[] arguments = new String[]{LocalDateTime.now().toString()};
SpringApplication.run(ReaderWriterProblem.class, arguments);
}
#Bean
public ItemWriter<Safety> safetyWriter() {
return new ItemWriter<Safety>() {
#Override
public void write(List<? extends Safety> items) throws Exception {
for (Safety item : items) {
//log.info(item.toString());
System.out.println(item);
}
}
};
}
// #Bean
// public JdbcCursorItemReader<Safety> cursorItemReader() throws Exception {
// JdbcCursorItemReader<Safety> reader = new JdbcCursorItemReader<>();
//
// reader.setSql("select * from safety ");
// reader.setDataSource(dataSource);
// reader.setRowMapper(new SafetyRowMapper());
// reader.setVerifyCursorPosition(false);
// reader.afterPropertiesSet();
//
// return reader;
// }
#Bean
JdbcPagingItemReader<Safety> safetyPagingItemReader() throws Exception {
JdbcPagingItemReader<Safety> reader = new JdbcPagingItemReader<>();
reader.setDataSource(dataSource);
reader.setFetchSize(10);
reader.setRowMapper(new SafetyRowMapper());
PostgresPagingQueryProvider queryProvider = new PostgresPagingQueryProvider();
queryProvider.setSelectClause("*");
queryProvider.setFromClause("safety");
Map<String, Order> sortKeys = new HashMap<>(1);
sortKeys.put("id", Order.ASCENDING);
queryProvider.setSortKeys(sortKeys);
reader.setQueryProvider(queryProvider);
return reader;
}
#Bean
public Step importSafetyDetails() throws Exception {
return stepBuilderFactory.get("importSafetyDetails")
.<Safety, Safety>chunk(5)
//.reader(cursorItemReader())
.reader(safetyPagingItemReader())
.writer(safetyWriter())
.listener(new MyStepListener())
.listener(new MyChunkListener())
.build();
}
#Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.listener(new JobListener())
.start(importSafetyDetails())
.build();
}
#Override
public void run(String... args) throws Exception {
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
jobParametersBuilder.addString("date", LocalDateTime.now().toString());
try {
Job job = (Job) context.getBean("job");
jobLauncher.run(job, jobParametersBuilder.toJobParameters());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
e.printStackTrace();
}
}
public static class JobListener implements JobExecutionListener {
#Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("Before job");
}
#Override
public void afterJob(JobExecution jobExecution) {
System.out.println("After job");
}
}
private static class SafetyRowMapper implements RowMapper<Safety> {
#Override
public Safety mapRow(ResultSet resultSet, int i) throws SQLException {
Safety safety = new Safety();
safety.setId(resultSet.getLong("ID"));
return safety;
}
}
public static class MyStepListener implements StepExecutionListener {
#Override
public void beforeStep(StepExecution stepExecution) {
System.out.println("Before Step");
}
#Override
public ExitStatus afterStep(StepExecution stepExecution) {
System.out.println("After Step");
return ExitStatus.COMPLETED;
}
}
private static class MyChunkListener implements ChunkListener {
#Override
public void beforeChunk(ChunkContext context) {
System.out.println("Before Chunk");
}
#Override
public void afterChunk(ChunkContext context) {
System.out.println("After Chunk");
}
#Override
public void afterChunkError(ChunkContext context) {
}
}
}
Hope this helps

How to use Spring boot AutoWired and ScheduledExecutorService?

I need to use autowired in more than one class with ScheduledExecutorService, what I have tried is shown in this code. logging size of User list in below example always shows 0, even after user added to arraylist. How to properly use Autowired and ScheduledExecutorService in spring boot?
#Component
public class AnotherClass {
List<User> users = new ArrayList();
public void addUser(User user){
users.add(user);
}
public void logUsers(){
logger.info("User size " + users.size()); <================= Always logs 0, when called from executor
}
}
#RestController
public class SecondClass {
#Autowired
private AnotherClass anotherClass;
#GetMapping(value="/user/test")
public void logUsers(){
anotherClass.addUser(new User());
}
}
Application Class
#Component
#SpringBootApplication
public class SpringBootDemoApplication {
private ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
#Autowired
private AnotherClass anotherClass;
#PostConstruct
public void init() {
logger();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
public void logger(){
exec.scheduleAtFixedRate(new Runnable(){
#Override
public void run(){
try {
anotherClass.logUsers();
}catch (Exception e){
}
}
}, 2000, 1000, TimeUnit.MILLISECONDS);
}
}
The code works if you use the Spring #Autowired and not the #AutoWired Annotation.

Spring Boot Isolation.SERIALIZABLE not working

I need help with this scenario, in theory the isolation level of Serializable should stop delete from happening, but in this scenario it still deletes the row with id 1, I have tried #EnableTransactionManagement and isolation repeatable read, it still doesn't block the delete nor cause the delete to throw exception
In summary, I need to stop any delete invocation whenever the update method is still ongoing
I am using H2 in memory database for this sample
Thanks
Entity:
public class Something {
#Id
private Integer id;
private String name;
private String desc;
}
Repo:
public interface SomeRepository extends JpaRepository<Something, Integer> {
}
Service:
#Service
public class SomeService {
#Autowired
private SomeRepository someRepository;
public void deleteSomething2(Something something) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
someRepository.delete(something);
}
#Transactional(isolation = Isolation.SERIALIZABLE)
public void updateSomething2(Something something) {
Something something1 = someRepository.findById(1).get();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
something1.setName("namanama");
someRepository.saveAndFlush(something1);
}
Test:
#RunWith(SpringRunner.class)
#SpringBootTest
public class DemoApplicationTests {
#Autowired
private SomeService service;
#Test
public void test() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<Future> futures = new ArrayList<>();
futures.add(executorService.submit(() -> service.updateSomething2(Something.builder().id(1).name("namaone").build())));
futures.add(executorService.submit(() -> service.deleteSomething2(Something.builder().id(1).build())));
while(futures.stream().filter(f -> f.isDone() == false).count() > 0) {
Thread.sleep(3000);
}
List<Something> all = service.findAll();
System.out.println(all);
}
}

Spring-Retry with Circuit breaker

I am trying to leverage both the retry and circuit breaker mechanism of spring-retry.
I tried to use both annotations(#Retryable and #CircuitBreaker) in a particular function(like below), but Circuit Breaker was not working.
#Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
#CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
#Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = #Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
#Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
Then I tried dividing the functionality into two different functions, both having #Retryable and #CircuitBreaker respectively. In this case, retry mechanism was not working. Please find below code snippet.
PS: exec method(Circuit Breaker method) is invoked from a controller.
#Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
#CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
public void exec() throws TypeOneException {
retryWhenException();
}
#Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = #Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
#Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
Can anyone please tell why it's behaving like this.
Also please advise if there exists a better way to implement both retry and circuit-breaker.
PS: I neither want to use resilience4j nor retryTemplate.
If you want retry within circuit breaker, they must be in different beans. If you call one #Retryable directly from another, in the same bean, you will bypass the interceptor.
This works fine for me...
#SpringBootApplication
#EnableRetry
public class So52193237Application {
public static void main(String[] args) {
SpringApplication.run(So52193237Application.class, args);
}
#Bean
public ApplicationRunner runner(Foo foo) {
return args -> {
try {
foo.exec();
}
catch (Exception e) {
try {
foo.exec();
}
catch (Exception ee) {
Thread.sleep(11000);
try {
foo.exec();
}
catch (Exception eee) {
}
}
}
};
}
#Component
public static class Foo {
private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
private final Bar bar;
public Foo(Bar bar) {
this.bar = bar;
}
#CircuitBreaker(maxAttempts = 1, openTimeout = 10000, resetTimeout=10000)
public void exec() throws TypeOneException {
LOGGER.info("Foo.circuit");
this.bar.retryWhenException();
}
#Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("Foo.recover");
throw t;
}
}
#Component
public static class Bar {
private static final Logger LOGGER = LoggerFactory.getLogger(Bar.class);
#Retryable(value = { TypeOneException.class }, maxAttempts = 3, backoff = #Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
#Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("Bar.recover");
throw t;
}
}
}

GWT + Spring + Hiberante. With no reason at all

I'm learning how to integrate Spring with GWT and RequestFactory by doing this following example. I got a NullPointerException and I don't know why. Can anyone help me?
Here is my code:
#Repository
public class EmployeeDAO implements IEmployeeDAO {
#PersistenceContext
private EntityManager entity;
#Override
public Employee findById(Long id) {
Query query = entity.createQuery("from Employee where id = :param");
query.setParameter("param", id);
query.setMaxResults(1);
return (Employee) query.getSingleResult();
}
#Transactional(propagation = Propagation.REQUIRED)
#Override
public void save(Employee employee) {
entity.merge(employee);
}
#Override
public void remove(Employee employee) {
entity.remove(employee);
}
#SuppressWarnings("unchecked")
#Override
public List<Employee> getAllEmployee() {
Query query = entity.createQuery("from Employee");
return query.getResultList();
}
// ...
}
and:
#Service(value = IEmployeeDAO.class, locator = DaoLocator.class)
public interface EmployeeRequestContext extends RequestContext {
Request<EmployeeProxy> findById(Long id);
Request<Void> save(EmployeeProxy employee);
Request<Void> remove(EmployeeProxy employee);
Request<List<EmployeeProxy>> getAllEmployee();
Request<EmployeeProxy> findOneByName(String name);
}
and:
#ProxyFor(Employee.class)
public interface EmployeeProxy extends EntityProxy {
Long getId();
String getName();
String getSurname();
void setId(Long id);
void setName(String name);
void setSurname(String surname);
Long getVersion();
void setVersion(Long version);
}
The NullPointerException is throw in GWT Entry Point in method:
protected void refresh() {
context = createFactory().employeeRequest();
final EmployeeProxy ep = context.create(EmployeeProxy.class);
ep.setName("Jan");
ep.setSurname("Kowalski");
ep.setVersion(new Long(0));
context.save(ep).fire(new Receiver<Void>() {
#Override
public void onSuccess(Void response) {
employeeList.add(ep);
}
#Override
public void onFailure(ServerFailure error) {
System.out.println("error podczas zapisu");
}
});
context = createFactory().employeeRequest();
context.getAllEmployee().fire(new Receiver<List<EmployeeProxy>>() {
#Override
public void onSuccess(List<EmployeeProxy> response) {
System.out.println(" " + response); // NULL
}
#Override
public void onFailure(ServerFailure error) {
}
});
System.out.println("Bedziemy wyswietlac dane!");
updateTable(employeeList);
}
the last one: method which create Factory:
private static EmployeeRequestFactory createFactory() {
EmployeeRequestFactory factory = GWT.create(EmployeeRequestFactory.class);
factory.initialize(new SimpleEventBus());
return factory;
}
Please help me...
Please print the stacktrace for the NullPointerException. Only then can we analyze the cause for the exception.

Resources