Using ConnectionManager to adapt to multi-database application environment
Source: Internet
Author: User
connection| Data | database
Original:
Today, in the enterprise application environment, more than one or a class of databases are to be confronted; On the other hand, different kinds of databases in different periods form a deserted island; in Java, the technology trend tends not to directly manipulate database records, but to achieve OO through persistent processing of database objects in the middle tier. Either way, it is required to be more easily confronted with multiple database connections. More than one database connection is familiar with the Microsoft ODBC data source settings, in addition to not achieve the middle tier persistence, ODBC is actually more comprehensive than JDBC and easier to handle. ConnectionManager is something that is set up through XML, similar to an ODBC data source setting, by defining DAO. Connection object, the program can be as needed: Java.sql.Connection conn =connectionmanager.getconnection (${connectionname}), get the required connection, This simplifies the editing work in multiple database environments.
Now the project developers are often faced with the application of multiple databases, history and different departments, there are CS backstage also have BS front desk. The data of each database must be merged into the same business logic, otherwise it becomes a low value data island. To cope with this demand, two technologies are used at present, one is data object technology, the essence is to insert a persistent object layer in the middle of data layer and application logic layer, the data object is consistent with data record through program or container, and the application logic accesses data object directly instead of database record. The second is to use multiple databases for data-object management use by using global catalog mobilization. Whether it's an EJB ENTITy, a hibernate, or a HANVA processor that I've written, it's a manifestation of this kind of thinking.
EJB is not always needed to manage multiple data sources, not to mention that most projects use EJB with higher cost and performance inefficiencies, more complex development work, even a chance to fill in code and publish no more code, and then try again, (I personally hate this sense of development, So we've got a Hanva project, unless the client specifies the EJB. However, the way in which multiple data sources are managed in the EJB container is well worth learning. In most cases, it is registered into the Jndi SPI by initializing the data source object, like a connection pool, and then by Java.naming.Context.lookup () the global name (within the JVM scope). My approach is to write a connectionmanager that defines multiple database connection-related parameters by an XML file, which is read into memory when the ConnectionManager is initialized. You can then get the specified database connection by calling the ConnectionManager static method getconnection (String connectionname) when you use it. Since the actual operation of the database connection is the processor and Lister two classes, so it is safe to ensure the release of the connection resources. Relying on this method, can be in the light of the application can be a lot of databases, the use of data-object method to develop.
Connections.xml definition:
<connection-set><description><! [Cdata[ connection-set for ConnectionManager use, you can take an indefinite number of connection definitions as needed, and the code will not change at all. In order to adapt to the use of multiple database environment;]]></description> <connection name= "Globalpool" driver= "DataSource" uri= "Java:comp" /env/globalpool "/> <connection name=" Mysqlpool driver= "datasource" uri= "Java:comp/env/mysqlpool"/> <connection name= "MySQL" driver= "Com.mysql.jdbc.Driver" Username= "Michelle" password= "Favordog" > <uri><! [cdata[jdbc:mysql://127.0.0.1:3306/dkt?useunicode=true&characterencoding=gb2312]]></uri> </connection> <connection name= "conn" driver= "Oracle.jdbc.driver.OracleDriver" Uri= "Jdbc:oracle:thin:@127.0.0.1:1521:dkt0" username= "Dcon" password= "Girlfriend"/> <connection name= " Reso "driver=" Oracle.jdbc.driver.OracleDriver " uri=" jdbc:oracle:thin:@127.0.0.1:1521:dkt0 "Username=" Erso "password=" abc "/> <connection name=" Daifu "driver=" OraclE.jdbc.driver.oracledriver " uri=" jdbc:oracle:thin:@127.0.0.1:1521:dkt0 "username=" EEF "password=" Qwewer "/> <connection name=" System "driver=" Oracle.jdbc.driver.OracleDriver " uri=" Jdbc:o Racle:thin:@127.0.0.1:1521:dkt0 "Username=" System "password=" manager "/></connection-set>
ConnectionManager Method:
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) initctx.lookup (Conn.geturi ()); cn = Ds.getconnection (); } else{ class.forname (DRI);//no newinstance (); only get class cn = JAva.sql.DriverManager.getConnection (Conn.geturi (), 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{ if (Dri.equalsignorecase ("datasource")) { javax.naming.context Initctx = new Javax.naming.InitialContext (); javax.sql.datasource ds = (javax.sql.DataSource) Initctx.lookup (Conn.geturi ()); &NBSP;&NBSP;&NBSP;&NBsp; cn = ds.getConnection (); } else{ class.forname (DRI);//no newinstance (); only get class cn = Java.sql.DriverManager.getConnection (Conn.geturi (), Conn.getusername (), Conn.getpassword ()); } }catch (Exception ex) { string msg = "dri:=" +dri+ "cn:="; +cn+ "+ 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) {&NBSP;&NBSP;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); }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service