1 10 11 package org.mule.extras.spring.transaction; 12 13 import org.mule.transaction.AbstractSingleResourceTransaction; 14 import org.mule.umo.TransactionException; 15 import org.mule.umo.UMOTransaction; 16 import org.mule.umo.UMOTransactionFactory; 17 import org.springframework.jdbc.datasource.ConnectionHolder; 18 import org.springframework.jms.connection.JmsResourceHolder; 19 import org.springframework.transaction.PlatformTransactionManager; 20 import org.springframework.transaction.TransactionStatus; 21 import org.springframework.transaction.support.TransactionSynchronizationManager; 22 23 26 public class SpringTransactionFactory implements UMOTransactionFactory 27 { 28 29 private PlatformTransactionManager manager; 30 31 public SpringTransactionFactory() 32 { 33 super(); 34 } 35 36 public UMOTransaction beginTransaction() throws TransactionException 37 { 38 return new SpringTransaction(); 39 } 40 41 public boolean isTransacted() 42 { 43 return true; 44 } 45 46 49 synchronized public PlatformTransactionManager getManager() 50 { 51 return manager; 52 } 53 54 57 synchronized public void setManager(PlatformTransactionManager manager) 58 { 59 this.manager = manager; 60 } 61 62 65 public class SpringTransaction extends AbstractSingleResourceTransaction 66 { 67 protected final TransactionStatus status; 68 69 public SpringTransaction() 70 { 71 status = manager.getTransaction(null); 72 } 73 74 protected void doBegin() throws TransactionException 75 { 76 } 78 79 protected void doCommit() throws TransactionException 80 { 81 manager.commit(status); 82 } 83 84 protected void doRollback() throws TransactionException 85 { 86 manager.rollback(status); 87 } 88 89 public Object getResource(Object key) 90 { 91 Object res = TransactionSynchronizationManager.getResource(key); 92 if (res != null) 93 { 94 if (!(res instanceof ConnectionHolder)) 95 { 96 if (res instanceof JmsResourceHolder) 97 { 98 return ((JmsResourceHolder)res).getConnection(); 99 } 100 } 101 else 102 { 103 return ((ConnectionHolder)res).getConnection(); 104 } 105 } 106 return res; 107 } 108 109 public boolean hasResource(Object key) 110 { 111 return getResource(key) != null; 112 } 113 114 public void bindResource(Object key, Object resource) throws TransactionException 115 { 116 throw new UnsupportedOperationException (); 117 } 118 119 public void setRollbackOnly() 120 { 121 super.setRollbackOnly(); 122 status.setRollbackOnly(); 123 } 124 125 } 126 127 } 128 | Popular Tags |