Transactional J(2)EE Applications  with Spring Transactional POJO Programming  Guy Pardon (guy@atomikos.com)
Contents <ul><li>Part 1: Spring Essentials </li></ul><ul><li>Part 2: Transactions in Spring </li></ul><ul><li>Part 3: JTA ...
Part 1 Spring Essentials
The Spring  Framework <ul><li>Inversion of Control (IoC) library </li></ul><ul><ul><li>Creation and configuration of objec...
Configuring a DataSource <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE be...
<ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE beans PUBLIC &quot;-//SPRIN...
Wiring Objects Together  <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE be...
Bank Class: “setDataSource”  <ul><li>public class Bank  </li></ul><ul><li>{ </li></ul><ul><li>private DataSource dataSourc...
Applications: Configuration No Longer Hard-Coded <ul><li>public class Main </li></ul><ul><li>{ </li></ul><ul><li>public st...
Spring Benefits <ul><li>Configuration is XML </li></ul><ul><ul><li>Can be changed without recompilation </li></ul></ul><ul...
Transactions in Spring? <ul><li>Can we use Spring to manage transactions? </li></ul><ul><li>Can we use the datasource mult...
Part 2 Transaction Support in Spring
Spring and Transactions <ul><li>Similar to EJB: </li></ul><ul><ul><li>Declarative or programmatic demarcation </li></ul></...
Transaction Strategies in Spring <ul><li>Strategy defines what underlying technology manages the transactions </li></ul><u...
Single-Connector Strategies <ul><li>The underlying transaction is actually a connector-specific transaction </li></ul><ul>...
JDBC Strategy <ul><li>The JDBC strategy needs the actual connection to commit/rollback on </li></ul><ul><ul><li>It must be...
Spring XML Config? <ul><li>A datasource bean </li></ul><ul><ul><li>Like before </li></ul></ul><ul><li>A proxy datasource <...
Example: Declarative + JDBC Strategy (1) <ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot;com.atomi...
Example: Declarative + JDBC Strategy (2) <ul><li><bean id=“ transactionManager ” class=“org.springframework.jdbc.datasourc...
JTA Strategy <ul><li>In this case, the underlying transaction is a JTA transaction </li></ul><ul><li>Essential if you have...
Example: Declarative + JTA Strategy (1) <ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot;com.atomik...
Example: Declarative + JTA Strategy (2) <ul><li><bean id=” transactionManager &quot; class=&quot;org.springframework.trans...
Using Other JTA Implementations <ul><li>Class org.springframework.transaction.jta.JtaTransactionManager has additional pro...
Transaction Attributes in Spring <ul><li>PROPAGATION_REQUIRED </li></ul><ul><li>PROPAGATION_REQUIRES_NEW </li></ul><ul><li...
Part 3 JTA Use Cases
Use Case 1: From Queue to DB Avoiding message loss and duplicates requires JTA! JMS JDBC Message Queue Message Listener Or...
Use Case 2: From Queue to Queue JMS Bridge Avoiding message loss and duplicates requires JTA! JMS JMS Source Queue Target ...
Use Case 3: From Web to DB and Queue Web Application Consistent Processing Requires JTA! JMS JDBC Message Queue Order Dept...
Use Case 4: Between 2 DBs Web Application Consistent Processing Requires JTA! JDBC JDBC Order Dept. DBMS HTTP Shipping Dep...
Conclusion: Spring Improves J2EE <ul><li>Enables J2EE without application server </li></ul><ul><ul><li>Components are free...
More Information? <ul><li>Read more in this article:  http://www.atomikos.com/Publications/J2eeWithoutApplicationServer </...
Upcoming SlideShare
Loading in...5
×

Developing Transactional JEE Apps With Spring

5,749

Published on

How to design and implement transactional J(2)EE applications with the Spring framework.

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,749
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
280
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Developing Transactional JEE Apps With Spring

  1. 1. Transactional J(2)EE Applications with Spring Transactional POJO Programming Guy Pardon (guy@atomikos.com)
  2. 2. Contents <ul><li>Part 1: Spring Essentials </li></ul><ul><li>Part 2: Transactions in Spring </li></ul><ul><li>Part 3: JTA Use Cases </li></ul>
  3. 3. Part 1 Spring Essentials
  4. 4. The Spring Framework <ul><li>Inversion of Control (IoC) library </li></ul><ul><ul><li>Creation and configuration of objects </li></ul></ul><ul><ul><ul><li>Keeps your application code free of class-specific configuration issues </li></ul></ul></ul><ul><ul><ul><li>Makes plain java components easier to write and use </li></ul></ul></ul><ul><ul><li>Dependency injection: wiring objects together </li></ul></ul><ul><ul><ul><li>Encourages programming against interfaces </li></ul></ul></ul><ul><ul><li>Advantage: allows clean and focused java components </li></ul></ul><ul><li>Supports aspect-oriented programming (AOP) </li></ul><ul><ul><li>Allows introduction of additional logic before/after a method call </li></ul></ul><ul><ul><li>Advantage: can replace typical appserver services </li></ul></ul><ul><li>Works both inside and outside of application server </li></ul>
  5. 5. Configuring a DataSource <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;> </li></ul><ul><li><beans> </li></ul><ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot; com.atomikos.jdbc.SimpleDataSourceBean &quot;> </li></ul><ul><li><property name=&quot;uniqueResourceName&quot;> </li></ul><ul><li> <value>XADBMS</value> </li></ul><ul><li> </property> </li></ul><ul><li><property name=&quot;xaDataSourceClassName&quot;> </li></ul><ul><li><value>COM.FirstSQL.Dbcp.DbcpXADataSource</value> </li></ul><ul><li> </property> </li></ul><ul><li> ... </li></ul><ul><li></bean> </li></ul><ul><li></beans> </li></ul>Ask Spring to create an object with this name and of this class, and to set the given properties
  6. 6. <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;> </li></ul><ul><li><beans> </li></ul><ul><li><bean id=&quot;datasource&quot; </li></ul><ul><li>class=&quot;com.atomikos.jdbc.SimpleDataSourceBean&quot;> </li></ul><ul><li> ... </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot; bank &quot; class= &quot; jdbc.Bank &quot;> </li></ul><ul><li>... </li></ul><ul><li></bean> </li></ul><ul><li></beans> </li></ul>A Client Component
  7. 7. Wiring Objects Together <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;> </li></ul><ul><li><beans> </li></ul><ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot;com.atomikos.jdbc.SimpleDataSourceBean&quot;> </li></ul><ul><li> ... </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot;bank&quot; class= &quot;jdbc.Bank&quot;> </li></ul><ul><li><property name=“dataSource”><ref bean=“ datasource ” /></property> </li></ul><ul><li></bean> </li></ul><ul><li></beans> </li></ul>Ask Spring to establish an association between the Objects called bank and datasource
  8. 8. Bank Class: “setDataSource” <ul><li>public class Bank </li></ul><ul><li>{ </li></ul><ul><li>private DataSource dataSource; </li></ul><ul><li>public Bank() {} </li></ul><ul><li>public void setDataSource ( DataSource dataSource ) </li></ul><ul><li>{ </li></ul><ul><li>this.dataSource = dataSource; </li></ul><ul><li>} </li></ul><ul><li>//the rest is pure JDBC code </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>Our Java code can now be independent of the actual JDBC vendor classes. This is called by the Spring wiring runtime.
  9. 9. Applications: Configuration No Longer Hard-Coded <ul><li>public class Main </li></ul><ul><li>{ </li></ul><ul><li>public static void main ( String[] args ) </li></ul><ul><li>throws Exception </li></ul><ul><li>{ </li></ul><ul><li>InputStream is = new FileInputStream(&quot;config.xml&quot;); </li></ul><ul><li>XmlBeanFactory factory = new XmlBeanFactory(is); </li></ul><ul><li>Bank bank = ( Bank ) factory.getBean ( &quot;bank&quot; ); </li></ul><ul><li> //the bank object is already configured and </li></ul><ul><li>ready to use!!! </li></ul><ul><li>bank.withdraw ( 10 , 100 ); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Just ask the Spring BeanFactory to use our XML configuration file. We can ask Spring to return configured objects by their name (id)
  10. 10. Spring Benefits <ul><li>Configuration is XML </li></ul><ul><ul><li>Can be changed without recompilation </li></ul></ul><ul><li>Configuration is centralized in files </li></ul><ul><ul><li>Easy to find and maintain </li></ul></ul><ul><li>Your Java code is pure and clean </li></ul><ul><ul><li>Easier to write and maintain </li></ul></ul><ul><ul><li>More focused on what it needs to do </li></ul></ul><ul><li>Your Java code has less dependencies </li></ul><ul><ul><li>Concrete class names are factored out </li></ul></ul><ul><li>We can control the wiring </li></ul><ul><ul><li>We can insert proxies that add services </li></ul></ul><ul><ul><li>You source code does not need to know </li></ul></ul><ul><li>Enables J2EE without appserver </li></ul>
  11. 11. Transactions in Spring? <ul><li>Can we use Spring to manage transactions? </li></ul><ul><li>Can we use the datasource multiple times and then still rollback in case of a failure? </li></ul><ul><li>Can we rollback across multiple connectors? </li></ul><ul><li>The rest of this presentation will discuss that </li></ul>
  12. 12. Part 2 Transaction Support in Spring
  13. 13. Spring and Transactions <ul><li>Similar to EJB: </li></ul><ul><ul><li>Declarative or programmatic demarcation </li></ul></ul><ul><ul><li>Several attributes for declarative transactions </li></ul></ul><ul><li>Different from EJB: </li></ul><ul><ul><li>Flexibility to choose the underlying transaction strategy </li></ul></ul><ul><ul><li>JDBC, JTA, JDO, Hibernate, ... </li></ul></ul><ul><ul><li>This means that you don’t necessarily need an application server to have transactions (J2EE without appserver)! </li></ul></ul>
  14. 14. Transaction Strategies in Spring <ul><li>Strategy defines what underlying technology manages the transactions </li></ul><ul><li>Single-connector strategies: </li></ul><ul><ul><li>JDBC </li></ul></ul><ul><ul><li>Hibernate </li></ul></ul><ul><ul><li>JDO </li></ul></ul><ul><ul><li>Other </li></ul></ul><ul><li>Multiple-connector strategy: </li></ul><ul><ul><li>JTA </li></ul></ul>
  15. 15. Single-Connector Strategies <ul><li>The underlying transaction is actually a connector-specific transaction </li></ul><ul><ul><li>JDBC: connection-level transaction </li></ul></ul><ul><ul><li>Hibernate: session-level transaction </li></ul></ul><ul><ul><li>JDO: session-level transaction </li></ul></ul><ul><li>Don’t use these if you have more than one connector! </li></ul><ul><ul><li>Only the JTA strategy is safe in that case </li></ul></ul>
  16. 16. JDBC Strategy <ul><li>The JDBC strategy needs the actual connection to commit/rollback on </li></ul><ul><ul><li>It must be aware of what connection your code is using </li></ul></ul><ul><ul><li>1 connection per transaction, reused every time </li></ul></ul><ul><li>Consequently, your code must either: </li></ul><ul><ul><li>Use Spring’s JDBC utilities (introduces code dependency on Spring), or </li></ul></ul><ul><ul><li>Access the datasource via Spring’s TransactionAwareDataSourceProxy (does not introduce code dependency on Spring: this is done via a proxy in the XML configuration) </li></ul></ul><ul><li>Similar implications apply to other single-connector strategies (but not to JTA strategy) </li></ul>
  17. 17. Spring XML Config? <ul><li>A datasource bean </li></ul><ul><ul><li>Like before </li></ul></ul><ul><li>A proxy datasource </li></ul><ul><ul><li>To wrap our datasource and return same connection within a transaction </li></ul></ul><ul><ul><li>Supplied to our bank </li></ul></ul><ul><li>An instance of the jdbc.Bank class </li></ul><ul><ul><li>To do the JDBC </li></ul></ul><ul><li>The transaction strategy </li></ul><ul><li>A proxy for the bank, that starts/ends a transaction for each method </li></ul><ul><ul><li>To insert the declarative transaction code </li></ul></ul><ul><ul><li>Wired with the transaction strategy </li></ul></ul><ul><li>No changes in our source code! </li></ul>
  18. 18. Example: Declarative + JDBC Strategy (1) <ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot;com.atomikos.jdbc.SimpleDataSourceBean&quot;> </li></ul><ul><li> ... </li></ul><ul><li></bean> </li></ul><ul><li><bean id=“ dsProxy ” </li></ul><ul><li>class=“org.springframework.jdbc.datasource. </li></ul><ul><li>TransactionAwareDataSourceProxy”> </li></ul><ul><li><property name=“targetDataSource”> <ref bean=“ datasource ” /></property> </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot; bankTarget &quot; class= &quot;jdbc.Bank&quot;> </li></ul><ul><li><property name=“dataSource”><ref bean=“ dsProxy ” /> </li></ul><ul><li></property> </li></ul><ul><li></bean> </li></ul>Vendor-specific datasource setup. Proxy required for JDBC strategy in Spring. Our bank’s JDBC is done via the proxy.
  19. 19. Example: Declarative + JDBC Strategy (2) <ul><li><bean id=“ transactionManager ” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”/> </li></ul><ul><li><property name=“dataSource”><ref bean=“ datasource ”></property> </li></ul><ul><li></bean> </li></ul><ul><li><bean id=“ bank ” </li></ul><ul><li>class=“org.springframework.transaction.interceptor. TransactionProxyFactoryBean”/> </li></ul><ul><li><property name=“transactionManager”><ref bean=“ transactionManager ”/></property> </li></ul><ul><li><property name=“target”><ref bean=“ bankTarget ”/></property> </li></ul><ul><li><property name=&quot;transactionAttributes&quot;> </li></ul><ul><li><props> </li></ul><ul><li><prop key=&quot;*&quot;>PROPAGATION_REQUIRED, -Exception</prop> </li></ul><ul><li></props> </li></ul><ul><li></property> </li></ul><ul><li></bean> </li></ul>Clients of our bank will use a proxy that inserts transaction logic. Transaction required, rollback on any exception.
  20. 20. JTA Strategy <ul><li>In this case, the underlying transaction is a JTA transaction </li></ul><ul><li>Essential if you have more than one connector to access </li></ul><ul><li>This does not introduce any dependencies in your code </li></ul><ul><li>The JTA strategy requires JTA-enabled DataSource instances </li></ul><ul><ul><li>Like those available for the appserver, or </li></ul></ul><ul><ul><li>Like those supplied by Atomikos for standalone use </li></ul></ul>
  21. 21. Example: Declarative + JTA Strategy (1) <ul><li><bean id=&quot; datasource &quot; </li></ul><ul><li>class=&quot;com.atomikos.jdbc.SimpleDataSourceBean&quot;> </li></ul><ul><li> ... </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot; bankTarget &quot; class= &quot;jdbc.Bank&quot;> </li></ul><ul><li><property name=“dataSource”><ref bean=“ datasource ” /></property> </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot; atomikosTM &quot; </li></ul><ul><li> class=&quot;com.atomikos.icatch.jta. UserTransactionManager&quot;> </li></ul><ul><li></bean> </li></ul><ul><li><bean id=&quot; atomikosUTX &quot; class=&quot;com.atomikos.icatch.jta.UserTransactionImp&quot;> </li></ul><ul><li></bean> </li></ul>Vendor-specific datasource setup (JTA enabled class). Setup Atomikos standalone JTA.
  22. 22. Example: Declarative + JTA Strategy (2) <ul><li><bean id=” transactionManager &quot; class=&quot;org.springframework.transaction.jta.JtaTransactionManager&quot;> </li></ul><ul><li><property name=&quot;transactionManager&quot;><ref bean=&quot; atomikosTM &quot; /></property> </li></ul><ul><li><property name=&quot;userTransaction&quot;><ref bean=&quot; atomikosUTX &quot; /></property> </li></ul><ul><li></bean> </li></ul><ul><li><bean id=“ bank ” </li></ul><ul><li>class=“org.springframework.transaction.interceptor. TransactionProxyFactoryBean”/> </li></ul><ul><li><property name=“transactionManager”><ref bean=“ transactionManager ”/></property> </li></ul><ul><li><property name=“target”><ref bean=“ bankTarget ”/></property> </li></ul><ul><li><property name=&quot;transactionAttributes&quot;> </li></ul><ul><li><props> </li></ul><ul><li><prop key=&quot;*&quot;>PROPAGATION_REQUIRED, -Exception</prop> </li></ul><ul><li></props> </li></ul><ul><li></property> </li></ul><ul><li></bean> </li></ul>Tell Spring to use the Atomikos JTA implementation.
  23. 23. Using Other JTA Implementations <ul><li>Class org.springframework.transaction.jta.JtaTransactionManager has additional properties: </li></ul><ul><ul><li>userTransactionName: JNDI name where usertransaction can be found (requires appserver) </li></ul></ul><ul><ul><li>transactionManagerName: JNDI name where transactionmanager can be found (requires appserver) </li></ul></ul><ul><li>Appserver-specific subclasses exist </li></ul><ul><ul><li>Optimized for specific application servers </li></ul></ul><ul><li>Conclusion: in principle, any JTA implementation can be used with Spring </li></ul><ul><ul><li>Use a standalone JTA if you want out-of-container applications </li></ul></ul>
  24. 24. Transaction Attributes in Spring <ul><li>PROPAGATION_REQUIRED </li></ul><ul><li>PROPAGATION_REQUIRES_NEW </li></ul><ul><li>PROPAGATION_SUPPORTS </li></ul><ul><li>PROPAGATION_NEVER </li></ul><ul><li>PROPAGATION_MANDATORY </li></ul><ul><li>PROPAGATION_NOT_SUPPORTED </li></ul><ul><li>PROPAGATION_NESTED </li></ul>
  25. 25. Part 3 JTA Use Cases
  26. 26. Use Case 1: From Queue to DB Avoiding message loss and duplicates requires JTA! JMS JDBC Message Queue Message Listener Order Dept. DBMS Spring Application
  27. 27. Use Case 2: From Queue to Queue JMS Bridge Avoiding message loss and duplicates requires JTA! JMS JMS Source Queue Target Queue
  28. 28. Use Case 3: From Web to DB and Queue Web Application Consistent Processing Requires JTA! JMS JDBC Message Queue Order Dept. DBMS HTTP
  29. 29. Use Case 4: Between 2 DBs Web Application Consistent Processing Requires JTA! JDBC JDBC Order Dept. DBMS HTTP Shipping Dept. DBMS
  30. 30. Conclusion: Spring Improves J2EE <ul><li>Enables J2EE without application server </li></ul><ul><ul><li>Components are freed of the EJB harness </li></ul></ul><ul><ul><li>CMT is still possible </li></ul></ul><ul><ul><li>Managed security is also possible </li></ul></ul><ul><ul><li>Less hardware resources needed </li></ul></ul><ul><ul><li>End of the deployment descriptor nightmare </li></ul></ul><ul><ul><li>Easier to develop, maintain, test and install </li></ul></ul><ul><li>Overall project cost is much lower, without loss of reliability </li></ul>
  31. 31. More Information? <ul><li>Read more in this article: http://www.atomikos.com/Publications/J2eeWithoutApplicationServer </li></ul><ul><li>Download Atomikos TransactionsEssentials </li></ul><ul><ul><li>http://www.atomikos.com/Main/TransactionsEssentials </li></ul></ul><ul><ul><li>Transaction management for Spring and JSE </li></ul></ul><ul><ul><li>A jar file that enables JTA in all your applications </li></ul></ul><ul><ul><li>Includes connection pooling (JDBC/JMS) </li></ul></ul><ul><ul><li>Includes message-driven JMS support for Spring </li></ul></ul><ul><ul><li>Downloadable from http://www.atomikos.com </li></ul></ul><ul><li>Contact me at [email_address] </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×