You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Ray Mitchell <rm...@teconline.com> on 2005/06/29 21:20:57 UTC
Transactions and multithreading
.NET iBatis users,
Hi, my name is Ray Mitchell. Thanks for any support you could give; I'm
working on my first project using iBatisNet.
Here's my question: Can multiple transactions take place in parallel
across different threads using a single DaoManager? I understand that
within a single thread a call to DaoManager.BeginTransaction must be
matched with either a call to DaoManager.CommitTransaction or
DaoManager.RollBackTransaction before another call to
DaoManager.BeginTransaction can be made. Is it possible to have
multiple threads call DaoManager.BeginTransaction on a single
DaoManager?
My tests indicate that multiple transactions CAN be in progress in
parallel using a single DaoManager. The following code works:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.Configuration;
using IBatisNet.DataAccess.Interfaces;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class IBatisTests
{
private static DaoManager mDaoManager;
[Test]
public void TestTransactions()
{
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure();
IBatisTests.mDaoManager =
DaoManager.GetInstance("IBatisDataMapperDaoContext");
for (int i = 0; i < 100; ++i)
{
IBatisTests.mDaoManager.BeginTransaction();
ThreadStart threadStart = new
ThreadStart(IBatisTests.MyThreadStart);
Thread workerThread = new Thread(threadStart);
workerThread.Start();
workerThread.Join();
IBatisTests.mDaoManager.CommitTransaction();
}
}
public static void MyThreadStart()
{
IBatisTests.mDaoManager.BeginTransaction();
IBatisTests.mDaoManager.CommitTransaction();
}
}
}
Does the DaoManager maintain a different IDalSession for each thread
that requests that a new transaction be started?
I need to know because I'd like to use a singleton DaoManager that is
used by multiple pages throughout a website. Each of these pages will
need to use transactions which will need to run in parallel.
Thanks,
Ray
Re: Transactions and multithreading
Posted by Gilles Bayon <ib...@gmail.com>.
The DaoManager is designed to be multi-threaded.
Each thread/page request will use a different transacation.
So yes, it is possible to have multiple threads call
DaoManager.BeginTransaction on a single DaoManager.