1 25 package org.ofbiz.entity.transaction; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 35 import org.enhydra.jdbc.standard.StandardXADataSource; 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.ObjectType; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.w3c.dom.Element ; 40 41 48 public class XaPoolConnectionFactory { 49 50 public static final String module = XaPoolConnectionFactory.class.getName(); 51 52 protected static Map dsCache = new HashMap (); 53 54 public static Connection getConnection(String helperName, Element jotmJdbcElement) throws SQLException , GenericEntityException { 55 StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache.get(helperName); 56 if (pds != null) { 57 if (Debug.verboseOn()) Debug.logVerbose(helperName + " pool size: " + pds.pool.getCount(), module); 58 return TransactionFactory.getCursorConnection(helperName, pds.getConnection()); 59 } 60 61 synchronized (XaPoolConnectionFactory.class) { 62 pds = (StandardXAPoolDataSource) dsCache.get(helperName); 63 if (pds != null) { 64 return pds.getConnection(); 65 } 66 67 String wrapperClass = jotmJdbcElement.getAttribute("pool-xa-wrapper-class"); 69 70 StandardXADataSource ds = null; 71 try { 72 ds = (StandardXADataSource) ObjectType.getInstance(wrapperClass); 74 pds = new StandardXAPoolDataSource(); 75 } catch (NoClassDefFoundError e) { 76 throw new GenericEntityException("Cannot find xapool.jar"); 77 } catch (ClassNotFoundException e) { 78 throw new GenericEntityException("Cannot load wrapper class: " + wrapperClass, e); 79 } catch (InstantiationException e) { 80 throw new GenericEntityException("Unable to instantiate " + wrapperClass, e); 81 } catch (IllegalAccessException e) { 82 throw new GenericEntityException("Problems getting instance of " + wrapperClass, e); 83 } 84 85 if (ds == null) 86 throw new GenericEntityException("StandardXaDataSource was not created, big problem!"); 87 88 ds.setDriverName(jotmJdbcElement.getAttribute("jdbc-driver")); 89 ds.setUrl(jotmJdbcElement.getAttribute("jdbc-uri")); 90 ds.setUser(jotmJdbcElement.getAttribute("jdbc-username")); 91 ds.setPassword(jotmJdbcElement.getAttribute("jdbc-password")); 92 ds.setDescription(helperName); 93 ds.setTransactionManager(TransactionFactory.getTransactionManager()); 94 95 String transIso = jotmJdbcElement.getAttribute("isolation-level"); 96 if (transIso != null && transIso.length() > 0) { 97 if ("Serializable".equals(transIso)) { 98 ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 99 } else if ("RepeatableRead".equals(transIso)) { 100 ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 101 } else if ("ReadUncommitted".equals(transIso)) { 102 ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 103 } else if ("ReadCommitted".equals(transIso)) { 104 ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 105 } else if ("None".equals(transIso)) { 106 ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_NONE); 107 } 108 } 109 110 pds.setDataSource(ds); 112 pds.setDescription(ds.getDescription()); 113 pds.setUser(ds.getUser()); 114 pds.setPassword(ds.getPassword()); 115 Debug.logInfo("XADataSource: " + ds.getClass().getName() + " attached to pool.", module); 116 117 pds.setTransactionManager(TransactionFactory.getTransactionManager()); 119 120 try { 122 pds.setMaxSize(new Integer (jotmJdbcElement.getAttribute("pool-maxsize")).intValue()); 123 pds.setMinSize(new Integer (jotmJdbcElement.getAttribute("pool-minsize")).intValue()); 124 pds.setSleepTime(new Long (jotmJdbcElement.getAttribute("pool-sleeptime")).longValue()); 125 pds.setLifeTime(new Long (jotmJdbcElement.getAttribute("pool-lifetime")).longValue()); 126 pds.setDeadLockMaxWait(new Long (jotmJdbcElement.getAttribute("pool-deadlock-maxwait")).longValue()); 127 pds.setDeadLockRetryWait(new Long (jotmJdbcElement.getAttribute("pool-deadlock-retrywait")).longValue()); 128 129 String testStmt = jotmJdbcElement.getAttribute("pool-jdbc-test-stmt"); 131 if (testStmt != null && testStmt.length() > 0) { 132 pds.setJdbcTestStmt(testStmt); 133 Debug.logInfo("Set JDBC Test Statement : " + testStmt, module); 134 } 135 } catch (NumberFormatException nfe) { 136 Debug.logError(nfe, "Problems with pool settings; the values MUST be numbers, using defaults.", module); 137 } catch (Exception e) { 138 Debug.logError(e, "Problems with pool settings", module); 139 } 140 141 dsCache.put(helperName, pds); 143 144 return TransactionFactory.getCursorConnection(helperName, pds.getConnection()); 145 } 146 } 147 148 public static void closeAll() { 149 Set cacheKeys = dsCache.keySet(); 150 Iterator i = cacheKeys.iterator(); 151 while (i.hasNext()) { 152 String helperName = (String ) i.next(); 153 StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache.remove(helperName); 154 pds.shutdown(true); 155 } 156 } 157 } 158 | Popular Tags |