0

See below spring boot code

I have used JPA repository.

  1. Controller.
  2. Service.
  3. Repository

BaseController

package com.controller;

import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BaseController {

    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/addStudent", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<String> base() {


        studentService.save();

        return new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
    }
}

StudentService.java

package com.service;

import com.model.Student;
import com.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    @Transactional
    public Student save() {

        Student student = new Student();
        student.setFirstName("ABC");
        student.setLastName("PQR");
        studentRepository.save(student);

        int i = 10 / 0;  //Error code

        return student;
    }
}

StudentRepository

package com.repository;

import com.model.Student;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("studentRepository")
public interface StudentRepository extends CrudRepository<Student, Long> {

    public List<Student> findAll();

}

Application.properties

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#maximum number of milliseconds that a client will wait for a connection
spring.datasource.hikari.connection-timeout = 20000
#minimum number of idle connections maintained by HikariCP in a connection pool
spring.datasource.hikari.minimum-idle= 10
#maximum pool size
spring.datasource.hikari.maximum-pool-size= 10
#maximum idle time for connection
spring.datasource.hikari.idle-timeout=10000
# maximum lifetime in milliseconds of a connection in the pool after it is closed.
spring.datasource.hikari.max-lifetime= 1000
#default auto-commit behavior.
spring.datasource.hikari.auto-commit =false

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/demo?autoReconnect=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.properties..hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update

After executing save method from StudentRepository, data get inserted immediately into database. no rollback or any other isolation levels are working in StudentServiceImpl.java even if Error code is there. I have tried to set "spring.datasource.hikari.auto-commit =true" setting value true, Placed @Transaction at top of the StudentServiceImpl.java class but still it didn't worked.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198

2 Answers2

0
  1. You do not need to mess with any hikari settings, and certainly not with autoCommit(true) as this DISABLES transactions. Delete all these properties.
  2. Where is the "error" in your code? Spring rolls back on unchecked exceptions being thrown (not checked ones or errors), I cannot see that in your code.
  3. What behavior do you expect? It looks fine to me.
Marco Behler
  • 3,627
  • 2
  • 17
  • 19
0

It's probably beacause of Open Jpa in View behaviour.
Write the following line in your properties file:

spring.jpa.open-in-view=false

take a look at this if you want to know more.

GionJh
  • 2,742
  • 2
  • 29
  • 68