SqlSessionManager vs SqlSessionFactory and SqlSession

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

SqlSessionManager vs SqlSessionFactory and SqlSession

rwfischer
Are there any guidelines when to use SqlSessionManager  and when to use SqlSessionFactory and SqlSession?

The examples on the webpage all use SqlSessionFactory and SqlSession.

The SqlSessionManager seems more convenient to use.

Is there any functional or performance impact of using one or the other?

Roger



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SqlSessionManager vs SqlSessionFactory and SqlSession

Guy Rouillier-2
SqlSessionManager is incredibly convenient compared to SqlSessionFactory.  Once SqlSessionManager became available, we switched to using it exclusively.  SqlSessionFactory was implemented earlier, which is why the examples all use it.  Both classes are pretty small, so you can easily browse the source.  SqlSessionManager actually implements SqlSessionFactory, so one does not significantly outpace the other on the performance front.

I'd suggest using SqlSessionManager unless you identify some compelling reason to use SqlSessionFactory.

--
Guy Rouillier



------ Original Message ------
From: "rwfischer" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 5/4/2017 12:27:12 AM
Subject: SqlSessionManager vs SqlSessionFactory and SqlSession

Are there any guidelines when to use SqlSessionManager  and when to use SqlSessionFactory and SqlSession?

The examples on the webpage all use SqlSessionFactory and SqlSession.

The SqlSessionManager seems more convenient to use.

Is there any functional or performance impact of using one or the other?

Roger



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

Virus-free. www.avast.com

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SqlSessionManager vs SqlSessionFactory and SqlSession

mybatis-user mailing list


On 4. May 2017, at 06:50, Guy Rouillier <[hidden email]> wrote:

SqlSessionManager is incredibly convenient compared to SqlSessionFactory.  Once SqlSessionManager became available, we switched to using it exclusively.  SqlSessionFactory was implemented earlier, which is why the examples all use it.  Both classes are pretty small, so you can easily browse the source.  SqlSessionManager actually implements SqlSessionFactory, so one does not significantly outpace the other on the performance front.

I'd suggest using SqlSessionManager unless you identify some compelling reason to use SqlSessionFactory.

Sorry Roger for hijacking your answer. First time I hear of SqlSessionManager. This is the pattern we have been working with for years.

try (SqlSession session = sqlSessionFactory.openSession()) {
SomeMapper mapper = session.getMapper(SomeMapper.class);
mapper.update(…)
session.commit();
}

Does SqlSessionManager improve this code? 


--
Guy Rouillier



------ Original Message ------
From: "rwfischer" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 5/4/2017 12:27:12 AM
Subject: SqlSessionManager vs SqlSessionFactory and SqlSession

Are there any guidelines when to use SqlSessionManager  and when to use SqlSessionFactory and SqlSession?

The examples on the webpage all use SqlSessionFactory and SqlSession.

The SqlSessionManager seems more convenient to use.

Is there any functional or performance impact of using one or the other?

Roger




-- 
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

Virus-free. www.avast.com

-- 
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SqlSessionManager vs SqlSessionFactory and SqlSession

rwfischer
Good question, Björn, because it prompted me to think a bit more about life-cycles. I am just starting with MyBatis.

As far as I understand:
  • SqlSessionManager simplifies session management for non-managed (auto-commit) transactions. You don't have to worry about creating and closing sessions.
  • For managed transactions, there is no difference. You have to use startManagedSession() to get a manged session, eventually call commit() or rollback(), and finally call close().

So, your code would look like this:

try {
sessionMgr.startManagedSession();
SomeMapper mapper = sessionMgr.getMapper(SomeMapper.class);
mapper.update(…)
sessionMgr.commit();
} finally {
sessionMgr.close();
}

Getting the session manager is also simpler.

Reader reader = Resources.getResourceAsReader(resource);
SqlSessionManager sessionMgr = SqlSessionManager.newInstance( reader, properties);

Roger

On Thursday, May 4, 2017 at 1:17:08 AM UTC-7, Björn Raupach wrote:

Sorry Roger for hijacking your answer. First time I hear of SqlSessionManager. This is the pattern we have been working with for years.

try (SqlSession session = sqlSessionFactory.openSession()) {
SomeMapper mapper = session.getMapper(SomeMapper.class);
mapper.update(…)
session.commit();
}

Does SqlSessionManager improve this code? 


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SqlSessionManager vs SqlSessionFactory and SqlSession

Kazuki Shimizu
In my understanding...
The {SqlSessionManager} provide the feature for sharing a {SqlSession} on same thread. Using this feature, we can inject and use same {SqlSessionManager} instance to any components without additional framework and library. Also, you can control a transaction centrally using facade class(e.g. servlet filter, front controller servlet, interceptor for AOP, etc...).

However I prefer using the Spring Framework integration(mybatis-spring or mybatis-spring-boot-starter) rather than the {SqlSessionManager}.

Thanks.


On Friday, May 5, 2017 at 6:20:04 AM UTC+9, rwfischer wrote:
Good question, Björn, because it prompted me to think a bit more about life-cycles. I am just starting with MyBatis.

As far as I understand:
  • SqlSessionManager simplifies session management for non-managed (auto-commit) transactions. You don't have to worry about creating and closing sessions.
  • For managed transactions, there is no difference. You have to use startManagedSession() to get a manged session, eventually call commit() or rollback(), and finally call close().

So, your code would look like this:

try {
sessionMgr.startManagedSession();
SomeMapper mapper = sessionMgr.getMapper(SomeMapper.class);
mapper.update(…)
sessionMgr.commit();
} finally {
sessionMgr.close();
}

Getting the session manager is also simpler.

Reader reader = Resources.getResourceAsReader(resource);
SqlSessionManager sessionMgr = SqlSessionManager.newInstance( reader, properties);

Roger

On Thursday, May 4, 2017 at 1:17:08 AM UTC-7, Björn Raupach wrote:

Sorry Roger for hijacking your answer. First time I hear of SqlSessionManager. This is the pattern we have been working with for years.

try (SqlSession session = sqlSessionFactory.openSession()) {
SomeMapper mapper = session.getMapper(SomeMapper.class);
mapper.update(…)
session.commit();
}

Does SqlSessionManager improve this code? 


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...