1 24 package org.ofbiz.entity.transaction; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import javax.transaction.TransactionManager ; 29 import javax.transaction.UserTransaction ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.entity.GenericEntityException; 33 import org.ofbiz.entity.config.DatasourceInfo; 34 import org.ofbiz.entity.config.EntityConfigUtil; 35 import org.ofbiz.entity.jdbc.CursorConnection; 36 37 44 public class TransactionFactory { 45 46 public static final String module = TransactionFactory.class.getName(); 47 public static TransactionFactoryInterface transactionFactory = null; 48 49 public static TransactionFactoryInterface getTransactionFactory() { 50 if (transactionFactory == null) { synchronized (TransactionFactory.class) { 52 if (transactionFactory == null) { 54 try { 55 String className = EntityConfigUtil.getTxFactoryClass(); 56 57 if (className == null) { 58 throw new IllegalStateException ("Could not find transaction factory class name definition"); 59 } 60 Class tfClass = null; 61 62 if (className != null && className.length() > 0) { 63 try { 64 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 65 tfClass = loader.loadClass(className); 66 } catch (ClassNotFoundException e) { 67 Debug.logWarning(e, module); 68 throw new IllegalStateException ("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage()); 69 } 70 } 71 72 try { 73 transactionFactory = (TransactionFactoryInterface) tfClass.newInstance(); 74 } catch (IllegalAccessException e) { 75 Debug.logWarning(e, module); 76 throw new IllegalStateException ("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage()); 77 } catch (InstantiationException e) { 78 Debug.logWarning(e, module); 79 throw new IllegalStateException ("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage()); 80 } 81 } catch (SecurityException e) { 82 Debug.logError(e, module); 83 throw new IllegalStateException ("Error loading TransactionFactory class: " + e.getMessage()); 84 } 85 } 86 } 87 } 88 return transactionFactory; 89 } 90 91 public static TransactionManager getTransactionManager() { 92 return getTransactionFactory().getTransactionManager(); 93 } 94 95 public static UserTransaction getUserTransaction() { 96 return getTransactionFactory().getUserTransaction(); 97 } 98 99 public static String getTxMgrName() { 100 return getTransactionFactory().getTxMgrName(); 101 } 102 103 public static Connection getConnection(String helperName) throws SQLException , GenericEntityException { 104 return getTransactionFactory().getConnection(helperName); 105 } 106 107 public static void shutdown() { 108 getTransactionFactory().shutdown(); 109 } 110 111 public static Connection getCursorConnection(String helperName, Connection con) { 112 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 113 if (datasourceInfo == null) { 114 Debug.logWarning("Could not find configuration for " + helperName + " datasource.", module); 115 return con; 116 } else if (datasourceInfo.useProxyCursor) { 117 try { 118 if (datasourceInfo.resultFetchSize > 1) 119 con = CursorConnection.newCursorConnection(con, datasourceInfo.cursorName, datasourceInfo.resultFetchSize); 120 } catch (Exception ex) { 121 Debug.logWarning(ex, "Error creating the cursor connection proxy " + helperName + " datasource.", module); 122 } 123 } 124 return con; 125 } 126 } 127 | Popular Tags |