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.ofbiz.base.util.Debug; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.minerva.pool.jdbc.xa.XAPoolDataSource; 37 import org.ofbiz.minerva.pool.jdbc.xa.wrapper.XADataSourceImpl; 38 import org.w3c.dom.Element ; 39 40 47 public class MinervaConnectionFactory { 48 49 public static final String module = MinervaConnectionFactory.class.getName(); 50 51 protected static Map dsCache = new HashMap (); 52 53 public static Connection getConnection(String helperName, Element jotmJdbcElement) throws SQLException , GenericEntityException { 54 XAPoolDataSource pds = (XAPoolDataSource) dsCache.get(helperName); 55 if (pds != null) { 56 return TransactionFactory.getCursorConnection(helperName, pds.getConnection()); 57 } 58 59 synchronized (MinervaConnectionFactory.class) { 60 pds = (XAPoolDataSource) dsCache.get(helperName); 61 if (pds != null) { 62 return pds.getConnection(); 63 } else { 64 pds = new XAPoolDataSource(); 65 pds.setPoolName(helperName); 66 } 67 68 XADataSourceImpl ds = new XADataSourceImpl(); 69 70 if (ds == null) 71 throw new GenericEntityException("XADataSource was not created, big problem!"); 72 73 ds.setDriver(jotmJdbcElement.getAttribute("jdbc-driver")); 74 ds.setURL(jotmJdbcElement.getAttribute("jdbc-uri")); 75 76 String transIso = jotmJdbcElement.getAttribute("isolation-level"); 77 if (transIso != null && transIso.length() > 0) { 78 if ("Serializable".equals(transIso)) { 79 pds.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 80 } else if ("RepeatableRead".equals(transIso)) { 81 pds.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 82 } else if ("ReadUncommitted".equals(transIso)) { 83 pds.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 84 } else if ("ReadCommitted".equals(transIso)) { 85 pds.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 86 } else if ("None".equals(transIso)) { 87 pds.setTransactionIsolation(Connection.TRANSACTION_NONE); 88 } 89 } 90 91 pds.setDataSource(ds); 93 pds.setJDBCUser(jotmJdbcElement.getAttribute("jdbc-username")); 94 pds.setJDBCPassword(jotmJdbcElement.getAttribute("jdbc-password")); 95 96 pds.setTransactionManager(TransactionFactory.getTransactionManager()); 98 99 try { 101 pds.setMaxSize(Integer.parseInt(jotmJdbcElement.getAttribute("pool-maxsize"))); 102 } catch (NumberFormatException nfe) { 103 Debug.logError("Problems with pool settings [pool-maxsize=" + jotmJdbcElement.getAttribute("pool-maxsize") + "]; the values MUST be numbers, using default of 20.", module); 104 pds.setMaxSize(20); 105 } catch (Exception e) { 106 Debug.logError(e, "Problems with pool settings", module); 107 pds.setMaxSize(20); 108 } 109 try { 110 pds.setMinSize(Integer.parseInt(jotmJdbcElement.getAttribute("pool-minsize"))); 111 } catch (NumberFormatException nfe) { 112 Debug.logError("Problems with pool settings [pool-minsize=" + jotmJdbcElement.getAttribute("pool-minsize") + "]; the values MUST be numbers, using default of 5.", module); 113 pds.setMinSize(2); 114 } catch (Exception e) { 115 Debug.logError(e, "Problems with pool settings", module); 116 pds.setMinSize(2); 117 } 118 119 dsCache.put(helperName, pds); 121 122 return TransactionFactory.getCursorConnection(helperName, pds.getConnection()); 123 } 124 } 125 126 public static void closeAll() { 127 Set cacheKeys = dsCache.keySet(); 128 Iterator i = cacheKeys.iterator(); 129 while (i.hasNext()) { 130 String helperName = (String ) i.next(); 131 XAPoolDataSource pds = (XAPoolDataSource) dsCache.remove(helperName); 132 pds.close(); 133 } 134 } 135 } 136 | Popular Tags |