Tutorials to .com

Tutorials to .com » Jsp » Xml » To adapt to multi-database environment ConnectionManager

To adapt to multi-database environment ConnectionManager

Print View , by: iSee ,Total views: 13 ,Word Count: 1332 ,Date: Mon, 20 Apr 2009 Time: 4:10 AM


Today, Enterprise application environments in general have to face, or a class of more than one database; on the other hand, different types at different stages of forming a database of the island; in java, technical trend tended to not direct the operation database records, and through the middle layer of the database to deal with persistent objects achieve the purpose of OO. No matter what kind of demand is easier to face a number of database connections. Comparison of a number of database connections is familiar Microsoft ODBC data source settings, apart from the absence of the middle layer lasting than corporatization, ODBC is much better than the JDBC comprehensive and easier to handle. ConnectionManager is a set through the XML, similar to ODBC data sources set up things, by the definition of the object dao.Connection, procedures may need to:

java.sql.Connection conn = ConnectionManager.getConnection ($ (connectionname));

Be required to connect, which simplifies a number of databases in the editing environment.

project developers are often faced with a number of database applications, history and different departments, there are also CS background BS prospects. All data must be integrated into the same business logic, or as a low value of the data island. Respond to this demand, the two technologies currently in use, one data object technology, in real terms is in the data layer and the application of the logic of the middle layer of persistent objects to insert a layer of containers through the procedure or data object consistent with the data record, and Application of the logic of direct access to data objects rather than database records; second is through the use of global catalog to mobilize the use of multiple databases for the management of the use of data objects. Both the EJB ENTITy, or Hibernate, or is it me who has written Hanva Processor is a reflection of this line of thinking.

EJB does not always need to manage multiple data sources, not to mention the majority of projects with the use of EJB is the higher cost and performance of the more inefficient, the development of more complex, and even luck to fill in the code and then release No luckreloading code, (I hate this development I am feeling Hanva therefore carried out a small item, unless the customer specified the EJB). However EJB container to manage multiple data sources or the methods are worth learning. In most cases, it is to initialize data source object, such as connection pooling, registered into JNDI SPI; then java.naming.Context.lookup () the global name (the scope of the JVM). My practice is to write a ConnectionManager, it is an xml document from a number of database connection related to the definition of the parameters, in ConnectionManager read into memory initialization. Use ConnectionManager and then by calling the static method getConnection (String connectionname), can be specified database connection. Database connection as a result of the operation actually Processor and Lister are two classes, so that connection to ensure the release of resources is highly uncertain. Rely on this method, you can light applications can properly engage in multi-database, use the method of data objects developed.

connections.xml definition:

connection-set for the use of ConnectionManager can be drawn as needed to connect the definition of a number of uncertainties, code completely unchanged. In order to adapt to the use of multi-database environment;
]]>

uri = "jdbc: oracle: thin: @ 127.0.0.1:1521: dkt0" username = "dcon" password = "girlfriend" />

uri = "jdbc: oracle: thin: @ 127.0.0.1:1521: dkt0" username = "erso" password = "abc" />

uri = "jdbc: oracle: thin: @ 127.0.0.1:1521: dkt0" username = "eef" password = "qwewer" />

uri = "jdbc: oracle: thin: @ 127.0.0.1:1521: dkt0" username = "system" password = "manager" />

ConnectionManager Methods:

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 ());

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 + "; 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);

)

)

Only need to call java.sql.Connection conn = ConnectionManager.getConnection ( "connn");

Can be specified on the form of database connection.


Java and XML Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.