Break a session into two transactions

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

Break a session into two transactions

rhummy
Hi everyone,

I am coding an example in order to get in touch with mybatis. And I
have a question.
It is about sqlsession and transaction. The user guide says that the
best scope for a sqlsession is request or method scope. I understand
that.
In fact, I just have services who instantiate mapper instances which
do little piece of work. And I want to group some of them inside a
transaction and another group inside another transaction. The two
transaction are inside the same service.

My code looks like what is suggested in the user guide :

**Beginning of the method or request**

SqlSession session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work
} finally {
  session.close();
}

**End of the method or request**

My question is : can I break my work into two session ? Is it a bad
idea ?
In code, can I write this :

**Beginning of the method or request**
SqlSession session;

session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work 1
  session.commit();
}catch (Exception Ex){
  session.rollback();
  throw new Exception();
} finally {
  session.close();
}

session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work 2
  session.commit();
}catch (Exception Ex){
  session.rollback();
  throw new Exception();
} finally {
  session.close();
}
**End of the method or request**

I want to do this because tables used in work 1 are very popular. And
I want to release them as soon as possible in order to let others to
do write on them.
Work 2 just read those tables. And they write in other tables.

Thank you for your answers.
Reply | Threaded
Open this post in threaded view
|

Re: Break a session into two transactions

Eduardo Macarron
Yes, I don´t see any problem in what you want to do.
Reply | Threaded
Open this post in threaded view
|

RE: Break a session into two transactions

Poitras Christian
In reply to this post by rhummy
Hi,

I don't see any problems in creating 2 instance of SqlSession.
We only recommend having one transaction per request/method since in 90% of the times, that is the best thing to do.

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de rhummy
Envoyé : October-11-10 9:55 PM
À : mybatis-user
Objet : Break a session into two transactions

Hi everyone,

I am coding an example in order to get in touch with mybatis. And I
have a question.
It is about sqlsession and transaction. The user guide says that the
best scope for a sqlsession is request or method scope. I understand
that.
In fact, I just have services who instantiate mapper instances which
do little piece of work. And I want to group some of them inside a
transaction and another group inside another transaction. The two
transaction are inside the same service.

My code looks like what is suggested in the user guide :

**Beginning of the method or request**

SqlSession session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work
} finally {
  session.close();
}

**End of the method or request**

My question is : can I break my work into two session ? Is it a bad
idea ?
In code, can I write this :

**Beginning of the method or request**
SqlSession session;

session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work 1
  session.commit();
}catch (Exception Ex){
  session.rollback();
  throw new Exception();
} finally {
  session.close();
}

session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  // do work 2
  session.commit();
}catch (Exception Ex){
  session.rollback();
  throw new Exception();
} finally {
  session.close();
}
**End of the method or request**

I want to do this because tables used in work 1 are very popular. And
I want to release them as soon as possible in order to let others to
do write on them.
Work 2 just read those tables. And they write in other tables.

Thank you for your answers.
Reply | Threaded
Open this post in threaded view
|

Re: Break a session into two transactions

rhummy
Thank you.

On Oct 12, 8:42 am, Poitras Christian <[hidden email]>
wrote:

> Hi,
>
> I don't see any problems in creating 2 instance of SqlSession.
> We only recommend having one transaction per request/method since in 90% of the times, that is the best thing to do.
>
> Christian
>
> -----Message d'origine-----
> De : [hidden email] [mailto:[hidden email]] De la part de rhummy
> Envoyé : October-11-10 9:55 PM
> À : mybatis-user
> Objet : Break a session into two transactions
>
> Hi everyone,
>
> I am coding an example in order to get in touch with mybatis. And I
> have a question.
> It is about sqlsession and transaction. The user guide says that the
> best scope for a sqlsession is request or method scope. I understand
> that.
> In fact, I just have services who instantiate mapper instances which
> do little piece of work. And I want to group some of them inside a
> transaction and another group inside another transaction. The two
> transaction are inside the same service.
>
> My code looks like what is suggested in the user guide :
>
> **Beginning of the method or request**
>
> SqlSession session = sqlSessionFactory.openSession();
> try {
>   BlogMapper mapper = session.getMapper(BlogMapper.class);
>   // do work
>
> } finally {
>   session.close();
> }
>
> **End of the method or request**
>
> My question is : can I break my work into two session ? Is it a bad
> idea ?
> In code, can I write this :
>
> **Beginning of the method or request**
> SqlSession session;
>
> session = sqlSessionFactory.openSession();
> try {
>   BlogMapper mapper = session.getMapper(BlogMapper.class);
>   // do work 1
>   session.commit();}catch (Exception Ex){
>
>   session.rollback();
>   throw new Exception();
>
> } finally {
>   session.close();
> }
>
> session = sqlSessionFactory.openSession();
> try {
>   BlogMapper mapper = session.getMapper(BlogMapper.class);
>   // do work 2
>   session.commit();}catch (Exception Ex){
>
>   session.rollback();
>   throw new Exception();} finally {
>   session.close();
> }
>
> **End of the method or request**
>
> I want to do this because tables used in work 1 are very popular. And
> I want to release them as soon as possible in order to let others to
> do write on them.
> Work 2 just read those tables. And they write in other tables.
>
> Thank you for your answers.