Java Reference
In-Depth Information
The dao.xml file is used by the DaoManagerBuilder to create a DaoManager
instance. Let's look at that next.
10.4.2
Creating a DaoManager instance
In order to use the DAO manager we have defined, we need to use a DaoManager-
Builder to create an instance of it. Listing 10.8 shows a fragment of code that you
can use to create a DaoManager instance.
Listing 10.8
Sample code to build a DaoManager
private DaoManager getDaoManager() {
DaoManager tempDaoManager = null;
Reader reader;
try {
reader = Resources.getResourceAsReader("Dao.xml");
tempDaoManager =
DaoManagerBuilder.buildDaoManager(reader);
} catch (Exception e) {
e.printStackTrace();
fail("Cannot load dao.xml file.");
}
return tempDaoManager;
}
Now that we have some code and configuration elements to look at, let's take a
closer look to see what is going on with all this stuff.
This code looks for a resource named Dao.xml , which is located at the root of
some location that the classloader will look in. For example, in Tomcat, it might
be in your web application's WEB-INF/classes directory or in a JAR file in its WEB-
INF/lib directory (as long as it was at the top level of the JAR file).
Once it has the configuration file, it passes the data to the DaoManagerBuilder ,
and requests that it build a DaoManager instance.
This code is from a JUnit test that we used to build and test the DAO implemen-
tations which we are looking at, so the exception handling is pretty weak. In a real
production application, this is not how you would want to handle the exception.
10.4.3
Defining the transaction manager
Next, we define a transaction manager that will be based on the transaction man-
ager we defined in our SQL Map configuration file, which we define using the
SqlMapConfigResource property nested in the <transactionManager> element. All
Search WWH ::




Custom Search