myBatis Support for Transactionality

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

myBatis Support for Transactionality

Matheen
Hi,

I am new to myBatis. In the current project that I am working in, we
have to access muliple databases and we need to keep that in
transaction. Can any one please guide me how to acheive this? What is
the best option.
One of the option is to have
org.springframework.jdbc.datasource.DataSourceTransactionManager
implement the transaction ( but this will be be used for only one
datasource) and other is Spring's PlatformTransactionManager.
Do I have any other options?

We are using tomcat, Spring 3 and myBatis 3.0.

Thanks,
Matheen
Reply | Threaded
Open this post in threaded view
|

Re: myBatis Support for Transactionality

nanlei1987
In our project, we have a layer called service, so the transaction is used for all method in the *Service class, so we configured multiple datasource connection pool to different databases. According to the each datasource, a sqlMapClientTemplate is set. We used Spring2 and iBatis2.

With the feature of Spring, the sqlMapClientTemplate is injected into the service class, so the class will have multiple sqlMapClientTemplate instances, the method defined in the class can invoke the method to deal with the DB operations.

So if exception happened in the service, all the execution will be rolled back.

example:
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="execution(* com.mycompany.service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>

Is there any other better solution for this issue, I want to know too.

Best Wishes!
Nan Lei

2010/10/10 Matheen <[hidden email]>
Hi,

I am new to myBatis. In the current project that I am working in, we
have to access muliple databases and we need to keep that in
transaction. Can any one please guide me how to acheive this? What is
the best option.
One of the option is to have
org.springframework.jdbc.datasource.DataSourceTransactionManager
implement the transaction ( but this will be be used for only one
datasource) and other is Spring's PlatformTransactionManager.
Do I have any other options?

We are using tomcat, Spring 3 and myBatis 3.0.

Thanks,
Matheen

Reply | Threaded
Open this post in threaded view
|

Re: myBatis Support for Transactionality

Eduardo Macarron
In reply to this post by Matheen
MyBatis just supports jdbc local transactions. For your scenario you
need a container (Spring, EJB...)

If you have more than one resource involved in a transaction you
should setup a JTA transaction manager in spring. AFAIK Tomcat does
not have XA (2PC) capabilities but there are opensource
implementations for that (have a look at JOTM for example)

And I do agree with Nan Lei. You should have a service layer for
demarcating transactions.

cheers!

On 10 oct, 16:26, Matheen <[hidden email]> wrote:

> Hi,
>
> I am new to myBatis. In the current project that I am working in, we
> have to access muliple databases and we need to keep that in
> transaction. Can any one please guide me how to acheive this? What is
> the best option.
> One of the option is to have
> org.springframework.jdbc.datasource.DataSourceTransactionManager
> implement the transaction ( but this will be be used for only one
> datasource) and other is Spring's PlatformTransactionManager.
> Do I have any other options?
>
> We are using tomcat, Spring 3 and myBatis 3.0.
>
> Thanks,
> Matheen
Reply | Threaded
Open this post in threaded view
|

Re: myBatis Support for Transactionality

Clinton Begin
Administrator
True, it only directly supports jDBC.

But yes, it can participate in a global transaction.  For that you'll need a container managed datasource and transaction.  You should set the transaction type to MANAGED for this, and retrieve the datasource from a JNDI name where your app server has bound the managed datasource. 

Cheers,
Clinton

On Mon, Oct 11, 2010 at 12:05 AM, Eduardo <[hidden email]> wrote:
MyBatis just supports jdbc local transactions. For your scenario you
need a container (Spring, EJB...)

If you have more than one resource involved in a transaction you
should setup a JTA transaction manager in spring. AFAIK Tomcat does
not have XA (2PC) capabilities but there are opensource
implementations for that (have a look at JOTM for example)

And I do agree with Nan Lei. You should have a service layer for
demarcating transactions.

cheers!

On 10 oct, 16:26, Matheen <[hidden email]> wrote:
> Hi,
>
> I am new to myBatis. In the current project that I am working in, we
> have to access muliple databases and we need to keep that in
> transaction. Can any one please guide me how to acheive this? What is
> the best option.
> One of the option is to have
> org.springframework.jdbc.datasource.DataSourceTransactionManager
> implement the transaction ( but this will be be used for only one
> datasource) and other is Spring's PlatformTransactionManager.
> Do I have any other options?
>
> We are using tomcat, Spring 3 and myBatis 3.0.
>
> Thanks,
> Matheen