spring 编程式事务

	PlatformTransactionManager txManager = new DataSourceTransactionManager(PersistModule.getDataSource());
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();  
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);  
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);  
	    TransactionStatus status = txManager.getTransaction(def); 
	  
	    StateLike result = null;
	    try {  
		    	completeState(currentState);
		    	result =run0(currentState);
		    	if(result != null){
		    		initNextState(result);
		    	}else{
		    		//流程结束,state位置成 complete
		    		updateProcessCurrentStatus(innerbizId,StateConstant.STATE_COMPLETE);//update  process table
		    	}
		    	
	           txManager.commit(status);
	    } catch (RuntimeException e) {  
	    		e.printStackTrace();
	        txManager.rollback(status);  
	    } catch (Throwable e) {
	    	
			e.printStackTrace();
		}  
	    
	    return  result;
第二种:模板模式

		 TransactionTemplate transactionTemplate = TemplateUtils.getTransactionTemplate();  
		 @SuppressWarnings("unchecked")
		 StateLike r = transactionTemplate.execute(new TransactionCallback() {  
	            public Object doInTransaction(TransactionStatus status) {  
	            	StateLike result = null;
	                try {  
		                	completeState(currentState);
			    		    	result =run0(currentState);
			    		    	if(result != null){
			    		    		initNextState(result);
			    		    	}else{
			    		    		//流程结束,state位置成 complete
			    		    		updateProcessCurrentStatus(innerbizId,StateConstant.STATE_COMPLETE);//update  process table
			    		    	}
	                		
			    		    	//NPE
//			    		    	Map m= null;
//			    		    	m.put("1", "1");
	                } catch (Throwable e) {  
	                    status.setRollbackOnly();  
	                    e.printStackTrace();  
	                }  
	                return result;  
	            }


	        });
		return r; 


其中

TemplateUtils为:::
public class TemplateUtils {
		private static Logger logger = Logger.getLogger(TemplateUtils.class);
		private static DataSource dataSource = null;
		private static PlatformTransactionManager platformTransactionManager = null;
		static {
			try {
				if(dataSource == null){
					dataSource = PersistModule.getDataSource();
				}
				
			} catch (Exception e) {
				logger.info("find PersistModule DataSource error···", e);
			}
		}

		public static TransactionTemplate getTransactionTemplate() {
			PlatformTransactionManager txManager = new DataSourceTransactionManager(
					dataSource);
			return new TransactionTemplate(txManager);
		}

		public static JdbcTemplate getJdbcTemplate() {
			return new JdbcTemplate(dataSource);
		}

		public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
			return new NamedParameterJdbcTemplate(dataSource);
		}

		public static SimpleJdbcTemplate getSimpleJdbcTemplate() {
			return new SimpleJdbcTemplate(dataSource);
		}

}








你可能感兴趣的:(spring 编程式事务)