During the development of a complex application, you may need to connect to more than one database
server. This requirement typically arises when you’re developing a replacement application for a legacy
system. The legacy applications has its own database servers, and you may have another one. The new
application code must communicate effectively with all the servers. Of course, you want to keep every-thing
working with Hibernate, so you must have a way to communicate with multiple database servers.
Fortunately, this isn’t a big deal to Hibernate, because it’s all in the configuration file(s). When you
instantiate a Configuration object, the system will by default attempt to find either a hibernate.properties
or a hibernate.cfg.xml file in the application’s root directory. One of these files contains the database con-figuration
information needed to contact a database server. To communicate with a second database
server, you need another configuration file.
For example, let’s say we have a MySQL database and a Microsoft SQL Server database that will be used
in an application. We’ll let the hibernate.cfg.xml file contain the connection and mapping information for
the MySQL database. For the Microsoft SQL Server database, we’ll build a configuration file called
sqlserver.cfg.xml, which looks like this:
<?xml version="I.0" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<hibernate-configuration>
<session-factory>
<property name="connection.driver class">
com.microsoft.jdbc.Driver</property> <property name="connection.url">
jdbc.Microsoft://localhost/products</property> <property
name="username">sa</property>
<property name="password">sa</property>
</session-factory>
</hibernate-configuration>
We now have two different configuration files for our databases. Next we create an application that uses
the files. Here’s an example piece of code:
Configuration configuration = new ConfigurationO.addClass(CD.class);
SessionFactory mysqlSession = configuration.buildSessionFactoryQ;
configuration = new Configuration().
configure("sglserver.cfg.xml").addClass(CD.class);
SessionFactory sqlserverSession = configuration.buildSessionFactoryO;
This code handles two different database servers. Remember, the key to using Hibernate is the
SessionFactory built from a Configuration object. The first part of the code creates a
Configuration object and a SessionFactory object using the technique from Chapter 3. This code
uses the default hibernate.cfg.xml file for its connection and mapping information.
The trick to using multiple databases comes into play in the second part of the code. Here we build a
new Configuration object, but we use a method called configure() to bring in the SQL Server con-figuration
file. The configure() method accepts an XML document that adheres to the hibernate-con-figuration-
2.0.dtd file. Our configuration file is designed to work with hibernate-configuration-2.0.dtd;
as such, the Configuration object gladly accepts it as a valid configuration file. Subsequently, the
object ignores the default configuration file.
server. This requirement typically arises when you’re developing a replacement application for a legacy
system. The legacy applications has its own database servers, and you may have another one. The new
application code must communicate effectively with all the servers. Of course, you want to keep every-thing
working with Hibernate, so you must have a way to communicate with multiple database servers.
Fortunately, this isn’t a big deal to Hibernate, because it’s all in the configuration file(s). When you
instantiate a Configuration object, the system will by default attempt to find either a hibernate.properties
or a hibernate.cfg.xml file in the application’s root directory. One of these files contains the database con-figuration
information needed to contact a database server. To communicate with a second database
server, you need another configuration file.
For example, let’s say we have a MySQL database and a Microsoft SQL Server database that will be used
in an application. We’ll let the hibernate.cfg.xml file contain the connection and mapping information for
the MySQL database. For the Microsoft SQL Server database, we’ll build a configuration file called
sqlserver.cfg.xml, which looks like this:
<?xml version="I.0" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<hibernate-configuration>
<session-factory>
<property name="connection.driver class">
com.microsoft.jdbc.Driver</property> <property name="connection.url">
jdbc.Microsoft://localhost/products</property> <property
name="username">sa</property>
<property name="password">sa</property>
</session-factory>
</hibernate-configuration>
We now have two different configuration files for our databases. Next we create an application that uses
the files. Here’s an example piece of code:
Configuration configuration = new ConfigurationO.addClass(CD.class);
SessionFactory mysqlSession = configuration.buildSessionFactoryQ;
configuration = new Configuration().
configure("sglserver.cfg.xml").addClass(CD.class);
SessionFactory sqlserverSession = configuration.buildSessionFactoryO;
This code handles two different database servers. Remember, the key to using Hibernate is the
SessionFactory built from a Configuration object. The first part of the code creates a
Configuration object and a SessionFactory object using the technique from Chapter 3. This code
uses the default hibernate.cfg.xml file for its connection and mapping information.
The trick to using multiple databases comes into play in the second part of the code. Here we build a
new Configuration object, but we use a method called configure() to bring in the SQL Server con-figuration
file. The configure() method accepts an XML document that adheres to the hibernate-con-figuration-
2.0.dtd file. Our configuration file is designed to work with hibernate-configuration-2.0.dtd;
as such, the Configuration object gladly accepts it as a valid configuration file. Subsequently, the
object ignores the default configuration file.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.