Jdbctemplate - How to commit and rollback?

Asked

Viewed 1,743 times

2

i am working with a Desktop application and use spring to realize my Inserts in the database.

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

I tried using Annotation @Transactional, but it doesn’t seem to work...

Actually, I don’t quite understand how it works... it reverses the commit?

What I’ve been trying to :

 @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')");
 }
  • What have you tried so far? Any problem error? Consider including what you are trying.

2 answers

2


Activating AOP

One of the things that confuses the use of annotations is that you often need to configure your framework to make it work properly.

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

To activate this in Spring, you first need to set a TransactionManager, for example:

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

Then 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 demarcating a transactional block with Spring is using the TransactionTemplate.

In the documentation has a very simple example, where just inject the 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();
            }
        });
    }
}

1

When you use Annotation @Transactional, you are saying that that component will take actions connecting to the database.

Now on to the attributes that make up the Annotation, they will dictate the behavior of the Annotation.

For further clarification on the @Transactional, check out here in the official documentation (in English).

In your case, I believe the following code snippet is in some kind of trouble.

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

Try changing the Annotation signature to:

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

On the functioning of @Transactional, it will only perform a rollback if you specify some behavior (such as an Exception) on rollbackFor.

Further, I hope I have been able to clarify some points about that Annotation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.