简体   繁体   中英

How to commit transaction in Spring with jdbc template ..?

i am new to Spring ..i was trying to do some database transactions using jdbc template....the programs are running fine but when i am fetching some data from database after some DML operations ,it is not showing updated data....after manually committing in the database the refreshed data are showing....Is there any way to commit the database from program it self using jdbc template....!! I am using Spring 4.1.6 version...Please help...

public void create(Student s){
        String sql = "insert into student(id,name,age)values(?,?,?)";
        jdbcTemplate.update(sql,s.getId(),s.getName(),s.getAge());
        System.out.println("Row Inserted");
        //Logic for commit...???
        return;
    }

Add @EnableTransactionManagement annotation to your config and mark the method as @Transactional .

Thus after exit the method transaction is committed (if no Exception was thrown)

See the example

@Transactional

在 dao 的每个方法上方添加这个符号。这将自动进行事务管理。

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