ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nguyen, Tom" <Tom.Ngu...@rels.info>
Subject RE: Transaction question
Date Fri, 11 May 2007 16:50:31 GMT
This is handled by iBatis through implementation of Session.

http://ibatis.apache.org/docs/dotnet/datamapper/ch04s04.html#id401597

Every time you call BeginTransaction, iBatis use the logic below to
handle session:

public IDalSession BeginTransaction()
{
    if (this._sessionHolder.LocalSession != null)
    {
        throw new DataMapperException ...snip...
    }
    SqlMapSession session = new SqlMapSession(this);
    this._sessionHolder.Store(session);
    session.BeginTransaction();
    return session;
}

 
Session Store is done through this logic:

public static ISessionStore GetSessionStore(string sqlMapperId)
{
    if (HttpContext.Current == null)
    {
        return new CallContextSessionStore(sqlMapperId);
    }
    return new WebSessionStore(sqlMapperId);
}

Details:

CallContext :
http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.messagi
ng.callcontext(vs.71).aspx

WebSession: uses HttpContext which is unique to http requests.

----------------------

Web is usually automatically handle for each users.  But if you are
doing multi-user in non-web with the same call context then you may have
to consider implementing your on thread safety session store.  But
otherwise, session handles transaction automatically.

Example of your question:

try
{
    sqlMap.BeginTransaction();
    Item item = (Item) sqlMap.QueryForObject("getItem", itemId);
    item.Description = newDescription;
    sqlMap.Update("updateItem", item);

    someObjectDAO.Save() // problem here

    sqlMap.CommitTransaction();
}
catch {
    sqlMap.RollBackTransaction();
}

-----------------

You will have to handle someObjectDAO.Save() in another session if you
don't have it to participate in the same session.

Something like someObjectDAO

public void Save()
{
	SqlMapSession session = new SqlMapSession(sqlMap);
	try {

		session.BeginTransaction();
		// do saving stuff here ...snip...
		session.CommitTransaction();
	} catch {
    		session.RollBackTransaction();
	}
}

Regards,

Tom Nguyen 
Sr. Developer
tom.nguyen@rels.info

-----Original Message-----
From: Mike Hill [mailto:hillmb@insightbb.com] 
Sent: Friday, May 11, 2007 10:48 AM
To: user-cs@ibatis.apache.org
Subject: Transaction question

Hello,

I'm new to iBatis .NET and had a quick question about how the 
session/transaction stuff should work.  The documentation examples shows

something like this:

try
{
    sqlMap.BeginTransaction();
    Item item = (Item) sqlMap.QueryForObject("getItem", itemId);
    item.Description = newDescription;
    sqlMap.Update("updateItem", item);
    sqlMap.CommitTransaction();
}
catch {
    sqlMap.RollBackTransaction();
}

Which works fine in this simple case where all of the transaction logic 
exists in the data layer.  In my case, I have several different Data 
Access Objects I've created (roughly one for each of my entity objects),

and from my business logic layer I want to call several methods across 
the different DAOs and have them all participate in a transaction.  Can 
someone guide me in the right direction?

All the DAO classes extend from a common base DAO which can easily 
begin/rollback/commit a transaction by getting an instance of the 
ISqlMapper and calling the appropriate methods.  However since the 
mapper is a singleton, and a transaction has been started, wouldn't this

then cause other DAO methods which may be called to inadvertently 
participate in the transaction since each DAO would be using the same 
ISqlMapper instance?

Thanks,
Mike


************************************************************************************
This e-mail message and any files transmitted herewith, are intended solely for the
use of the individual(s) addressed and may contain confidential, proprietary or 
privileged information.  If you are not the addressee indicated in this message 
(or responsible for delivery of this message to such person) you may not review, 
use, disclose or distribute this message or any files transmitted herewith.  If you 
receive this message in error, please contact the sender by reply e-mail and delete
this message and all copies of it from your system.
************************************************************************************

Mime
View raw message