Explicit open session for read-only queries with SqlSessionManager

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

Explicit open session for read-only queries with SqlSessionManager

rwfischer
When using the SqlSessionManager for read-only queries, is it recommended to:

1: Use an implicit session. When the session manager is created, a session proxy is also created.

2: Explicitly open and close a session?

3) Use a managed session?


Example for 1 (implicit session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
SanMapper m = sessMgr.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
...


Example for 2 (open/close session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
SqlSession sess = sessMgr.openSession();
SanMapper m = sess.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
sess
.close();
...


Example 3 (managed session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
sessMgr
.startManagedSession();
SanMapper m = sessMgr.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
sessMgr
.close();
...

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: Explicit open session for read-only queries with SqlSessionManager

Guy Rouillier-2
Use (1) whenever possible.  The major point of SqlSessionManager is to take care of all the SqlSession housekeeping for you.  Use (3) when required to perform multiple statements in a single transaction.  I can't think of a scenario where you'd using option (2).  I just checked a code base that uses SqlSessionManager extensively, and couldn't find any instances where we used openSession().

Hope that helps.

--
Guy Rouillier



------ Original Message ------
From: "rwfischer" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 5/17/2017 1:15:40 AM
Subject: Explicit open session for read-only queries with SqlSessionManager

When using the SqlSessionManager for read-only queries, is it recommended to:

1: Use an implicit session. When the session manager is created, a session proxy is also created.

2: Explicitly open and close a session?

3) Use a managed session?


Example for 1 (implicit session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
SanMapper m = sessMgr.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
...


Example for 2 (open/close session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
SqlSession sess = sessMgr.openSession();
SanMapper m = sess.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
sess
.close();
...


Example 3 (managed session):

SqlSessionManager sessMgr = SqlSessionManager.newInstance(mapperReader, props);
...
sessMgr
.startManagedSession();
SanMapper m = sessMgr.getMapper(SanMapper.class);
San s = m.selectByPrimaryKey(1);
sessMgr
.close();
...

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.
Loading...