JdbcTemplate - How to commit and rollback?

2

I'm working with a Desktop application and I use spring to hold my inserts in the database.

The object I'm using is the JdbcTemplate, which has the encapsulated datasource inside it. I would like to know how to start new transactions with JdbcTemplate and how I can commit and rollback a transaction using this object.

I tried to use @Transactional annotation, but it does not seem to work ...

Actually, I did not quite understand how it works ... does it reverse the commit?

What I've been trying:

 @Transactional(rollingbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
 public void Testing(){
    jdbcTemplate.exec("Insert into table Acess_Level(IdLevel,Description) values(1,'Admin')");
    jdbcTemplate.exec("Insert into table Acess_Level(IdLevel,Description) values(STRING,'Admin')");
 }
    
asked by anonymous 28.01.2016 / 20:17

2 answers

2

Enabling AOP

One of the things you confuse in using annotations is that you often need to configure your framework for them to function properly.

See, for Spring to be able to intercept the method call to then demarcate a transaction, it uses an Aspect Oriented Programming type.

To enable this in Spring, you first need to define a TransactionManager , for example:

<bean id="myTransactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
      scope="singleton">
    <property name="dataSource" ref="myDataSource" />
</bean>

Next, set the setting to enable AOP in Spring:

<tx:annotation-driven transaction-manager="myTransactionManager" />

If you use Spring Boot or configuration via classes, just put the equivalent settings in your configuration class. See documentation here .

TransactionTemplate

Annotation is not the only way to use transactions. The programmatic version of marking a transactional block with Spring is using TransactionTemplate .

In the documentation has an example very simple, where you just need to inject TransactionManager and instantiate the transaction object:

public class SimpleService implements Service {

    // single TransactionTemplate shared amongst all methods in this instance
    private final TransactionTemplate transactionTemplate;

    // use constructor-injection to supply the PlatformTransactionManager
    public SimpleService(PlatformTransactionManager transactionManager) {
        Assert.notNull(transactionManager, "The ''transactionManager'' argument must not be null.");
        this.transactionTemplate = new TransactionTemplate(transactionManager);
    }

    public Object someServiceMethod() {
        return transactionTemplate.execute(new TransactionCallback() {
            // the code in this method executes in a transactional context
            public Object doInTransaction(TransactionStatus status) {
                updateOperation1();
                return resultOfUpdateOperation2();
            }
        });
    }
}
    
29.01.2016 / 03:10
1

When you use the @Transactional annotation, you are saying that that component will have database connection actions.

Now about the attributes that compose annotation, they will dictate annotation behavior.

For more information on @Transactional , check out here in the official documentation.

In your case, I believe that the following code snippet has a problem.

@Transactional(rollingbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)

Try changing the annotation signature to:

@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)

On the operation of @Transactional , it will only rollback if you specify some behavior (such as Exception) in rollbackFor .

I hope to have been able to clarify some points about this annotation.

    
29.01.2016 / 02:26