Java Reference
In-Depth Information
this maintains only a single connection that's reused all the time and never closed. Obviously, it is not
suitable in a multithreaded environment.
Spring's own data source implementations are mainly used for testing purposes. However, many
production data source implementations support connection pooling. For example, the Database
Connection Pooling Services (DBCP) module of the Apache Commons Library has several data source
implementations that support connection pooling. Of these,
BasicDataSource
accepts the same
connection properties as
DriverManagerDataSource
and allows you to specify the initial connection
size and maximum active connections for the connection pool.
■
Note To use the data source implementations provided by DBCP, you have to include
commons-dbcp.jar
and
commons-pool.jar
(located in the
lib/jakarta-commons
directory of the Spring installation) in your classpath.
<bean id="dataSource"
class="
org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:
derby://localhost:1527/vehicle;c
reate=true" />
<property name="username" value="app" />
<property name="password" value="app" />
<property name="initialSize" value="2" />
<property name="maxActive" value="5" />
</bean>
Many Java EE application servers build in data source implementations that you can configure from
the server console. If you have a data source configured in an application server and exposed for JNDI
lookup, you can use
JndiObjectFactoryBean
to look it up.
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/VehicleDS" />
</bean>
In Spring, a JNDI lookup can be simplified by the
jndi-lookup
element defined in the
jee
schema.
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="
http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/VehicleDS" />
...
</beans>
Search WWH ::
Custom Search