简体   繁体   中英

Spring boot commandlinerunner not committing transaction

I have built a utility to load some data using spring boot, the utility is able to open connection to the DB and pull data, but it is not committing the data somehow. The following is the code.

I ran it in the following ways and none worked.

java -jar target/someapp-load-dt-0.0.1-SNAPSHOT.jar
mvn spring-boot:run

code

@SpringBootApplication
public class SomeApplication implements CommandLineRunner {

@Autowired
DataProcessor dataProcessor;

public static void main(String[] args) {
    SpringApplication.run(BroadsoftLoadDtApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    this.dataProcessor.process();
}
}

DataProcessor.java

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Service
public class DataProcessor {
    @Autowired
    private SomeDao bshServicePackDAO;

    @Transactional(readOnly = false)
    public void process() {
        bshServicePackDAO.findAll();//gets the data fine
        bshServicePackDAO.save(new SomeDto(1,2,3));
    }
}

SomeDao.java

@Repository
public interface SomeDao extends CrudRepository<SomeDto, Integer> {
}

application.properties

#Basic Spring Boot Config for Oracle
spring.datasource.url= jdbc:oracle:thin:@DBL.LOCAL:1521/test
spring.datasource.username=test
spring.datasource.password=xya
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

I had @JoinTable with many to many mapping, i had to change the owner of the relationship. The commits started working. In the past the Two.java had @ManytoMany mapping and the owner was Two, i had to change it to the following for it to work. now one.voiceFeatures.add(Two); and commit is working fine.

Class One.java

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "join_table", schema = "int", joinColumns = {
        @JoinColumn(name = "service_pack_id", referencedColumnName = "service_pack_id", nullable = false)
}, inverseJoinColumns = {
        @JoinColumn(name = "voice_feature_id", referencedColumnName = "voice_feature_id", nullable = false)
})
public Set<Two> voiceFeatures;

in Two.java

@ManyToMany(mappedBy = "voiceFeatures", fetch = FetchType.LAZY)
public Set<One> servicePacks;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM