Malli,
Transactions should be applied to business objects like POJOs.
A
word of clarification. JTS (Java Transaction Service) is the API used by server vendors to supply transaction services. Unless you are writing an application server, you do not need to know anything about it. JTA (Java Transaction API) is the API used by application programmers. If you decide to implement transactions programmatically, this is what you would use in a J2EE environment. J2EE application servers (not webapp containers) are required to implement JTS to provide support for Global Transactions (i.e. transactions that can be used against multiple resources like databases, JMS messaging systems or other systems using JCA Java Connector Architecture). Normally you will also have to use JNDI to get a UserTransaction in this environment.
There are many applications, i.e. ones that use a single transactional resource like a database, that don't need to use JTA or an application server. These services can be obtained directly from the database using a
JDBC Connection to commit or rollback programmatically.
Spring provides the ability to specify transactions declaratively in XML configuration files or through metadata attributes. Spring's AOP framework provides the TranasctionProxyFactoryBean to wrap your POJO in transaction handling code to make the process very easy to implement. Spring supports local (using PlatfromTransactionManager) or global tranasctions (using JTATransactionManager) as a simple item you can specify in your configuration file. If you elect to use a global strategy, you will still need a JTA service provider like a full blown application server. If you only use a single database, you can use the local transaction strategy and run your webapp in
Tomcat. this allows you to achieve a lot of what EJB has to offer without having to resort to writing and testing EJBs. These transaction strategies can be applied to any of the persistence strategies that Spring supports, JDBC, Hibernate, IBatis SQLMaps, JDO, Apache OJB, and etc. It looks like support for Cayenne and Toplink ORM's is also forthcoming as well as Hibernate3. Even if you don't want to use an ORM, Spring's JDBC support make it ultra simple to write reliable code that doesn't leak connections.
Again I urge you to take a look at the Spring docs chapter 7. There are good examples there that show you how to set up your application to take care of this as well as a good explanation of transaction management in general.
[ January 24, 2005: Message edited by: Ken Krebs ]
[ January 24, 2005: Message edited by: Ken Krebs ]