Database Connections and DataSources

First of all, you can use Spring to manage the database connection for you by providing a bean that implements javax.sql.DataSource. The difference between a DataSource and a Connection is that a DataSource provides and manages Connections.

DriverManagerDataSource is the simplest implementation of a DataSource. By looking at the class name, you can guess that it simply calls the DriverManager to obtain a connection. This makes DriverManagerDataSource unsuitable for anything other than testing. The configuration of DriverManagerDataSource is quite simple, as you can see in Listing 8-3; you just need to supply the driver class name, a connection URL, a user name, and a password.

Listing 8-3: Spring-Managed DriverManagerDataSource dataSource Bean

image from book
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean                    destroy-method="close">         <property name="driverClassName">              <value>org.postgresql.Driver</value></property>         <property name="url">              <value>jdbc:postgresql://localhost/janm</value></property>         <property name="username"><value>janm</value></property>         <property name="password"><value></value></property>     </bean> </beans>
image from book

In real-world applications you can use Apache Jakarta Commons BasicDataSource (http://jakarta.apache.org/commons/dbcp/) or a DataSource implemented by a J2EE application server, which may further increase the performance of the application. You could use a DataSource in the plain JDBC code and get the same pooling benefits; however, in most cases, you would still miss a central place to configure the data source. Spring, on the other hand, allows you to declare a dataSource bean and set the connection properties in the application context definition files (see Listing 8-4).

Listing 8-4: Spring-Managed dataSource Bean

image from book
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>          <bean            destroy-method="close">         <property name="driverClassName">              <value>org.postgresql.Driver</value></property>         <property name="url">              <value>jdbc:postgresql://localhost/janm</value></property>         <property name="username"><value>janm</value></property>         <property name="password"><value></value></property>     </bean> </beans>
image from book

Most likely you recognize the bold properties in the listing. They represent the values you normally pass to JDBC to obtain a Connection interface. This particular Spring-managed DataSource is implemented in org.apache.commons.dbcp.BasicDataSource. The most important bit is that the dataSource bean implements javax.sql.DataSource, and you can immediately start using it in your data access classes.

Another way to configure a dataSource bean is to use JNDI. If the application you are developing is going to run in a J2EE container, you can take advantage of the container-managed connection pooling. To use a JNDI-based data source, you need to change the dataSource bean declaration, as shown in Listing 8-5.

Listing 8-5: Spring-Managed JNDI dataSource Bean

image from book
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean           bold">org.springframework.jndi.JndiObjectFactoryBean">         <property name="jndiName">             <value>java:comp/env/jdbc/springch8</value>         </property>     </bean> </beans>
image from book

If you take the JNDI approach, you must not forget to add a resource reference (resource-ref) in the application descriptor file (see Listing 8-6).

Listing 8-6: A Resource Reference in Descriptor Files

image from book
<root-node>     <resource-ref>         <res-ref-name>jdbc/springch8</res-ref-name>         <res-type>javax.sql.DataSource</res-type>         <res-auth>Container</res-auth>     </resource-ref> </root-node>
image from book

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 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/springch8 reference name and that the dataSource bean's jndiName is set to java:comp/env/jdbc/springch8.

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 data source 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 to.

The connection management is also delegated to the dataSource bean, which in turn performs the management itself or uses the J2EE container to do all the work.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net