<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</root-node>
The <root-node> is a placeholder value; you need to change it depending on how your module is
packaged. For example, it becomes <web-app> in the web deployment descriptor (WEB-INF/web.xml) if the
application is a web module. Most likely, you will need to configure the resource-ref in an application
server­specific descriptor file as well. However, notice that the resource-ref element configures the
jdbc/prospring3ch8 reference name and that the dataSource bean's jndiName is set to
java:comp/env/jdbc/prospring3ch8.
As you can see, Spring allows you to configure the DataSource in almost any way you like, and it
hides the actual implementation or location of the datasource from the rest of the application's code. In
other words, your DAO classes do not know and do not need to know where the DataSource points.
The connection management is also delegated to the dataSource bean, which in turn performs the
management itself or uses the JEE container to do all the work.
Embedded Database Support
Starting from version 3.0, Spring also offers embedded database support, which automatically starts an
embedded database and exposes it as a DataSource for the application. Listing 8-16 shows the
configuration of an embedded database (app-context-xml.xml).
Listing 8-16. Spring Embedded Database Support
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
</beans>
In the previous listing, we first declared the jdbc namespace in the <beans> tag. Afterward, we use
the <jdbc:embedded-database> to declare the embedded database and assign it with an ID of dataSource.
Within the tag, we also instruct Spring to execute the scripts specified to create the database schema and
populate testing data accordingly. Note that the order of the scripts is important, and the file that
contains Data Definition Language (DDL) should always appears first, followed by the file with Data
Manipulation Language (DML). For the type attribute, we specify the type of embedded database to use.
As of version 3.1, Spring supports HSQL (default), H2, and DERBY.
The embedded database support is extremely useful for local development or unit testing.
Throughout the rest of this chapter, we will use the embedded database to run the sample code, so your
machine doesn't require a database to be installed in order to run the samples.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home