original:
Today's enterprise application environment is generally facing more than one or a type of database; on the other hand, different types of databases form a desert island; in Java, technology trend tends to not directly operate database records, and It is the purpose of the OO through the perspective of the database object persistence. No matter that, it is required to easily connect multiple databases. A plurality of database connections are familiar to Microsoft's ODBC data source settings, in addition to not achieving intermediate layer persistence, ODBC is actually more easier than JDBC. ConnectionManager is set by XML, similar to the ODBC data source setting, by defining the DAO.CONNECTION object, the program can be as needed: java.sql.connection conn = connectionsManager.getConnection ($ {connectionname}); get what you need Connection, thereby simplifying editing in multiple database environments.
Now the project developers are often used by multiple database applications, history and different departments, and there is also a BS front desk in the background of the CS. Data of each database must be integrated into the same business logic, otherwise it will become a low-value data island. There are two technologies, and the data objectification technology is currently used. The substance is inserted into a persistent object in the middle of the data layer and the application logic layer. The data object is consistent with the data record through the program or container. The application logic directly accesses the data object instead of database records; the other is to use the global directory to use multiple databases for data objectization management. Whether it is EJB Entity, or Hibernate, or the Hanva Processor I have written is an embodiment of this idea.
Don't always need to use EJB to manage multiple data sources, let alone Most of the EJB tapes are higher cost and performance more efficient, development work more complicated, even just fill the code and release the code I can't fill the code and then touch the luck, (I am very annoying this kind of development, so I have made a hanva small project unless the customer specifies EJB). However, the way to manage multiple data sources in the EJB container is also worth learning. In most cases, it is through the initialized data source object, like connecting pool, registers into JNDI SPI; then through java.naming.context.lookup () global name (within JVM range). My approach is written by writing a ConnectionManager, which defines multiple database connection related parameters by an XML file, and reads memory when ConnectionManager is initialized. When using the static method getConnection (String ConnectionName), you can get the specified database connection by calling the ConnectionManager's static method (String ConnectionName). Because it is actually two classes of Processor and Lister, it is also very horrified to ensure that the release of connection resources is also very horrified. Relying on this method, you can do a multi-database library in light applications, using data objectification methods.
Connections.xml definition:
public class ConnectionManager {private static LOGGER logger = (LOGGER) Loger.getLogger (Constants.LOGGER_DAIFU_KEY);. public static synchronized java.sql.Connection getConnection () throws java.sql.SQLException {String name = Repository.getInstance () getConn ( Return getConnection (Name);
public static synchronized java.sql.Connection getConnection (String name) throws java.sql.SQLException {if (Repository.getInstance () == null) {logger.error ( "the repository for dao operation has not been initialized"); return NULL;} dao.connection conn = repository.getinstance (). getConnection (name); string dri = conn.getdriver (); java.sql.connection cn = null; try {if (Dri.EqualsignoreCase ("Datasource") {Javax.naming.context initctx = new javax.naming.initialcontext (); javax.sql.datasource ds = (javax.sql.datasource) initX.lookup (conn.geturi ()); cn = ds.getConnection (); } Else {class.forname (DRI); // no newinstance (); Only get class cn = java.sql.drivermanager.getConnection (), conn.getuserName (), conn.getpassword ());} } catCH (Exception EX) {String MSG = "ConnectionManager.class, Conname: =" Name "; DRI: =" DRI "; CN: =" CN "; MSG: =" ex.getMessage (); Logger.Error (MSG, ConnectionManager.class); throw new java.sql.sqlexception (msg);} return cn;} public static synchronized java.sql.Connection getConnection (dao.Connection conn) throws java.sql.SQLException {if (conn == null) {System.out.println ( "the dao.Connection to getConnection is null;" Return null;} String dri = conn.getdriver (); java.sql.connection cn = null; try = null; "Datasource") {javax.naming.context initcture = new javax.naming. InitialContext (); javax.sql.datasource ds = (javax.sql.datasource) initX.lookup (conn.geturi ()); cn = ds.getConnection ();} else {class.forname (DRI); // no NEWINSTANCE ();
Only get class cn = java.sql.driverManager.getConnection (), conn.getusername (), conn.getpassword ();}}}}} catCH (Exception EX) {string msg = "dri: =" DRI "; CN: =" CN "; MSG: =" ex.getMessage (); logger.error (msg, connectionManager.class); throw new java.sql.sqlexception (msg);} return cn;} public static void main (string [] args) throws exception {if (args == null || args.length <2) {system.out.println ("please input the xml and conn's name"); return;} repository.parse Args [0]); java.sql.connection conn = getConnection (Args [1]); System.out.Println (CONN);}}
Just need to call
Java.sql.connection conn = connectionsManager.getConnection ("conn");
You can get the database connection to the specified manner.