1 6 package org.jfox.jdbc.xa; 7 8 import java.sql.Connection ; 9 import java.util.Map ; 10 import java.util.WeakHashMap ; 11 12 import org.jfox.ioc.logger.Logger; 13 14 19 20 public class XADataSourceManager { 21 22 private static Map dataSources = new WeakHashMap (); 23 private static Logger logger = Logger.getLogger(XADataSourceManager.class.getName()); 24 25 public static void registerDataSource(String name, TxDataSource txds) { 26 logger.debug("registerDataSource " + name + " => " + txds); 27 dataSources.put(name, txds); 28 } 29 30 public static void unregisterDataSource(String name) { 31 logger.debug("unregisterDataSource " + name); 32 dataSources.remove(name); 33 } 34 35 public static TxDataSource getDataSource(String name) { 36 logger.debug("getDataSource " + name); 37 if(!existsDataSource(name)) { 38 logger.warn("XADataSource " + name + " is not exists!"); 39 return null; 40 } 41 return (TxDataSource) dataSources.get(name); 42 } 43 44 public static TxDataSource createDataSource(String dsName, String dbUrl, String user, String password) { 45 return createDataSource(dsName, dbUrl, user, password, Connection.TRANSACTION_READ_COMMITTED); 46 } 47 48 53 public static TxDataSource createDataSource(String dsName, String dbUrl, String user, String password, int transactionIsolate) { 54 logger.debug("createDataSource [DataSourceName=" + dsName + ", URL=" + dbUrl + ", user=" + user + ", password=<hidden>, TransactionIsolate=" + transactionIsolate + "]"); 55 if(existsDataSource(dsName)) { 56 TxDataSource txds = getDataSource(dsName); 57 if(!txds.getDbUrl().equals(dbUrl) 58 || !txds.getUser().equals(user) 59 || !txds.getPassword().equals(password) 60 || txds.getTransactionIsolation() != transactionIsolate) { 61 logger.warn("can not create DataSource, " + dsName + " is exists, but not with same configuration"); 62 return null; 63 } 64 txds.setTransactionIsolation(transactionIsolate); 65 return txds; 66 } 67 else { 68 TxDataSource tds = newDataSource(dbUrl, user, password); 69 tds.setTransactionIsolation(transactionIsolate); 70 registerDataSource(dsName, tds); 71 return tds; 72 } 73 } 74 75 public static TxDataSource newDataSource(String dbUrl, String user, String password) { 76 return newDataSource(dbUrl, user, password, Connection.TRANSACTION_READ_COMMITTED); 77 } 78 79 84 public static TxDataSource newDataSource(String dbUrl, String user, String password, int transactionIsolate) { 85 logger.debug("createDataSource [URL=" + dbUrl + ", user=" + user + ", password=<hidden>, TransactionIsolate=" + transactionIsolate + "]"); 86 TxDataSource txds = new TxDataSource(dbUrl, user, password); 87 txds.setTransactionIsolation(transactionIsolate); 88 return txds; 89 } 90 91 public static boolean existsDataSource(String name) { 92 if(name == null) { 93 return false; 94 } 95 return dataSources.containsKey(name); 96 } 97 98 public static int getDataSourceCount() { 99 return dataSources.size(); 100 } 101 102 public static void main(String [] args) { 103 104 } 105 } 106 107 | Popular Tags |