99 views
Skip to first unread message

Ravi Thapa

unread,
Jul 29, 2012, 9:39:19 AM7/29/12
to mybati...@googlegroups.com

If i open the session

SqlMapSession session = sqlMap.openSession()
and if does not close the session explicitly, what are the consequences, does ibatis or Mybatis framework takes care of closing the session.  


Jeff Butler

unread,
Jul 29, 2012, 10:34:35 AM7/29/12
to mybati...@googlegroups.com
If you use the raw MyBatis API like this then you must close the session. MyBatis will not close it for you. The consequences can be uncommitted transactions and connection leaks. 

I think a better pattern is to use the Spring or Guice support.  In that case the framework will handle everything for you. 

Jeff Butler

Guy Rouillier

unread,
Jul 29, 2012, 11:07:57 PM7/29/12
to mybati...@googlegroups.com
We use SqlSessionManager, which also has great, nearly transparent
session management.

On 7/29/2012 10:34 AM, Jeff Butler wrote:
> If you use the raw MyBatis API like this then you must close the
> session. MyBatis will not close it for you. The consequences can be
> uncommitted transactions and connection leaks.
>
> I think a better pattern is to use the Spring or Guice support. In that
> case the framework will handle everything for you.
>
> Jeff Butler
>
> On Sunday, July 29, 2012, Ravi Thapa wrote:
>
>
> If i open the session
>
> *SqlMapSession session = sqlMap.openSession()*
>
> and if does not close the session explicitly, what are the
> consequences, does ibatis or Mybatis framework takes care of closing
> the session.
>
>


--
Guy Rouillier

Ravi Thapa

unread,
Jul 29, 2012, 11:49:47 PM7/29/12
to mybati...@googlegroups.com
Thanks Jeff.
--
Thanks and Regards

Ravi Thapa

unread,
Jul 30, 2012, 1:30:13 PM7/30/12
to mybati...@googlegroups.com

Can anyone show me how to use start transaction and End transaction in Mybatis or Ibatis 3.0.

I am not finding any proper example of handling transactions.
 
Thanks and Regards

Ravi Thapa

unread,
Jul 30, 2012, 1:33:23 PM7/30/12
to mybati...@googlegroups.com

How to handle Execute batch, For example Batch update or batch insert in Ibatis 3.0 or Mybatis. 

sqlMap.queryForList("getFiredEmployees", null);
   sqlMap.startBatch ();
   for (int i=0, n=list.size(); i < n; i++) {
     sqlMap.delete ("deleteEmployee", list.get(i));
   }
   sqlMap.executeBatch();

Is this the way to run the queries in batch. please let me know
--
Thanks and Regards

Eduardo Macarron

unread,
Jul 30, 2012, 2:00:58 PM7/30/12
to mybati...@googlegroups.com
from:
http://www.mybatis.org/core/java-api.html#sqlSessions

SqlSession session = sqlSessionFactory.openSession();
try {
// following 3 lines pseudocod for "doing some work"
session.insert(...);
session.update(...);
session.delete(...);
session.commit();
} finally {
session.close();
}

2012/7/30 Ravi Thapa <ravit...@gmail.com>:

Eduardo Macarron

unread,
Jul 30, 2012, 2:02:55 PM7/30/12
to mybati...@googlegroups.com
again in:
http://www.mybatis.org/core/java-api.html#sqlSessions

have a look at openSession method's doc.

2012/7/30 Ravi Thapa <ravit...@gmail.com>:

Ravi Thapa

unread,
Jul 31, 2012, 1:22:28 PM7/31/12
to mybati...@googlegroups.com
I went through the document
<settings>
  <setting name="cacheEnabled" value="true"/>
  <setting name="lazyLoadingEnabled" value="true"/>
  <setting name="multipleResultSetsEnabled" value="true"/>
  <setting name="useColumnLabel" value="true"/>
  <setting name="useGeneratedKeys" value="false"/>
  <setting name="autoMappingBehavior" value="PARTIAL"/>
  <setting name="defaultExecutorType" value="SIMPLE"/>
  <setting name="defaultStatementTimeout" value="25000"/>
  <setting name="safeRowBoundsEnabled" value="false"/>
  <setting name="mapUnderscoreToCamelCase" value="false"/>
  <setting name="localCacheScope" value="SESSION"/>
  <setting name="jdbcTypeForNull" value="OTHER"/>
  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>

When I define <setting name="defaultExecutorType" value="BATCH"/> IN THE SQLCONFIG.XML, whether this indicates that whenever if i do updates or inserts in bulk , this will automatically execute all the updates or inserts in one go and fire in database, is this my correct understanding Eduardo??

say suppose find my below example

for(looping){
// Insert records in database
sqlsession.insert();
}


I have logged the sql statements which runs(like insert,update,delete,select) in the log file. But when i define the above setting in the the config file and if i do bulk update[refer above for loop] then the update query which would be fired does not log in the log file.

What you say on this?? Please let me know.




Eduardo Macarron

unread,
Jul 31, 2012, 1:38:59 PM7/31/12
to mybati...@googlegroups.com
I should be logged. Note that batches execute when:
- commit, close, flushStatements is called
- a different update statement is executed
- a query is executed

Try calling commit and post you log in both cases. You should see the batches executed.

Larry Meadors

unread,
Jul 31, 2012, 2:08:12 PM7/31/12
to mybati...@googlegroups.com
Also (slightly off-topic), if you put a subject on your emails you
might get better help. I know when I see one that comes in as "(no
subject)", I figure if you can't email, you might have bigger problems
than using java and mybatis. :-)

Larry
Reply all
Reply to author
Forward
0 new messages