PDA

View Full Version : declarative transaction management examples?



mediumo
10-14-2006, 04:33 PM
Hi,

Can anyone recommend a good example or reference regarding the use of declarative transaction management? I'm fairly new to Spring.NET, and for that matter quite new to enterprise design patterns (I missed the Java boat!).

Thanks,
Jon

Mark Pollack
10-16-2006, 07:22 AM
Hi,

I'll be writing docs this weel so that should help ;) In the meantime, take a look in the directory test\Spring\Spring.Data.Integration.Tests\Data in the nightly download. The files AutoDeclartiveTxTets.cs and autoDeclarativeServices.xml should hopefully get you started.

Cheers,
Mark

salibhai
04-22-2008, 06:53 PM
Thanks Mark. I myself am getting my feet wet with Spring.NET for the first time.

Here is a code snippet from Spring.Data.TransactionTemplateTests.cs


[Test]
public void ExecuteTransactionManager()
{
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.PropagationBehavior = TransactionPropagation.Required;

//TODO change to property of name TransactionStatus...
ITransactionStatus status = transactionManager.GetTransaction(def);

int iCount = 0;
try
{
iCount = (int)adoOperations.ExecuteScalar(CommandType.Text, "SELECT COUNT(*) FROM TestObjects");
/*
IAdoCommand cmd = new AdoCommand(dbProvider, CommandType.Text);
cmd.CommandText = "SELECT COUNT(*) FROM TestObjects";
iCount = (int)cmd.ExecuteScalar();
*/

//other AdoCommands can be executed within same tx.
} catch (Exception e)
{
transactionManager.Rollback(status);
throw e;
}
transactionManager.Commit(status);
Assert.AreEqual(2, iCount);

}

Mark Pollack
04-22-2008, 08:01 PM
Hi,

Wow, quite the old thread that was revived. There is now an example application distributed with the release that covers transaction management. Look under examples\Spring\Spring.TxQuickStart (or the menu item if you used the installer) and follow the corresponding documentation (http://www.springframework.net/doc-latest/reference/html/tx-quickstart.html).

TransactionTemplate is used for programmatic transaction management (http://www.springframework.net/doc-latest/reference/html/transaction.html#transaction-programmatic) but more commonly one will use declarative transaction management (http://www.springframework.net/doc-latest/reference/html/transaction.html#dtm), either through XML or attribute based configuration.

Cheers,
Mark

salibhai
05-02-2008, 01:24 PM
Hi there.

We successfully implemented spring in our application!

I wrote a post about it - Managing your transaction chaos with Spring Framework (http://www.sharpdeveloper.net/content/archive/2008/04/30/manage-your-transaction-chaos-with-spring-framework.aspx)

Thanks for your help Mark!

paduris
05-02-2008, 03:17 PM
Hi Mark,

I’m using the declarative transaction management with Hibernate template but transactions are not roll back for multiple tables when expectation occurred. Can you please recommend a good example or reference regarding the declarative transaction management with Hibernate template?

We are currently using Sping.Net 1.1 Rc1.

Thanks in Advance,
sri

Mark Pollack
05-14-2008, 09:06 PM
Hi Sri,

Please update to the latest version of Spring (1.1.2). You can find some example code for NHibernate in the Northwind web application that is in the distribution.

Cheers,
Mark

Mark Pollack
05-14-2008, 09:18 PM
@salibhai (http://forum.springframework.net/member.php?u=9071)

Thanks for writing about Spring.NET. Just to be clear, that the example you show on your blog is not using declarative transaction management, but rather programmatic transaction management based on Spring's transaction managment abstraction, IPlatformTransactionManager.

Declarative transaction management means that you specify the transaction semantics using metadata, either in the form of attributes or in XML. If you take a look at the code that is in the transactions quickstart (http://www.springframework.net/doc-latest/reference/html/tx-quickstart.html), that is in the form of using declarative transaction management with attributes.

The code shown would have much less code in it when using declarative transaction management, i.e.



public class TestObjectDao : AdoDaoSupport, ITestObjectDao
{
public int GetCount()
{
return (int)AdoTemplate.ExecuteScalar(CommandType.Text, "SELECT COUNT(*) FROM TestObjects");
}
}
AdoDaoSupport is a simple utility base class (not even really needed, you could provide your own) that will initialize the AdoTemplate property based on a IDbProvider instance. Transaction management would be done at the service layer, i.e.


[Transaction]
public class ServiceLayer : IServiceLayer
{

// dao properties and field definitions omitted.

public void DoWork()
{
int count = TestObjectDao.GetCount();

// other work with other daos will participate in same transaction
}
}
Spring would manage the data access class and service layer class. Those would be defined in the spring xml file along with an instance of a IPlatformTransactionManager, most likely AdoPlatformTransactionManager. To enable declarative transaction management, i.e. to tell spring to recognize the [Transaction] attribute on the service layer class, just add the following bit to the XML and register the tx namespace parser.



<tx:attribute-driven/>
Cheers,
Mark