petclinic-uncached

SPR-6447 + extracted datasource definition into a separate

11/26/2009 6:04:27 PM

Changes

src/main/java/org/springframework/samples/petclinic/config/DbcpDataSourceFactory.java 261(+0 -261)

Details

diff --git a/src/main/resources/jdbc.properties b/src/main/resources/jdbc.properties
index 02bda07..802ff63 100644
--- a/src/main/resources/jdbc.properties
+++ b/src/main/resources/jdbc.properties
@@ -22,10 +22,8 @@ jdbc.username=sa
 jdbc.password=
 
 # Properties that control the population of schema and data for a new data source
-jdbc.populate=true
-jdbc.schemaLocation=classpath:db/hsqldb/initDB.txt
+jdbc.initLocation=classpath:db/hsqldb/initDB.txt
 jdbc.dataLocation=classpath:db/hsqldb/populateDB.txt
-jdbc.dropLocation=classpath:db/hsqldb/dropTables.txt
 
 # Property that determines which Hibernate dialect to use
 # (only applied with "applicationContext-hibernate.xml")
@@ -43,14 +41,12 @@ jpa.database=HSQL
 
 #jdbc.driverClassName=com.mysql.jdbc.Driver
 #jdbc.url=jdbc:mysql://localhost:3306/petclinic
-#jdbc.username=pc
-#jdbc.password=pc
+#jdbc.username=root
+#jdbc.password=
 
 # Properties that control the population of schema and data for a new data source
-#jdbc.populate=false
 #jdbc.schemaLocation=classpath:db/mysql/initDB.txt
 #jdbc.dataLocation=classpath:db/mysql/populateDB.txt
-#jdbc.dropLocation=classpath:db/mysql/dropTables.txt
 
 # Property that determines which Hibernate dialect to use
 # (only applied with "applicationContext-hibernate.xml")
diff --git a/src/main/webapp/WEB-INF/applicationContext-dataSource.xml b/src/main/webapp/WEB-INF/applicationContext-dataSource.xml
new file mode 100644
index 0000000..cf655c5
--- /dev/null
+++ b/src/main/webapp/WEB-INF/applicationContext-dataSource.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	Application context definition for PetClinic Datasource.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+		xmlns:p="http://www.springframework.org/schema/p"
+		xmlns:context="http://www.springframework.org/schema/context" 
+		xmlns:jdbc="http://www.springframework.org/schema/jdbc"
+		xsi:schemaLocation="
+			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+			http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
+				
+    <!-- ========================= DATASOURCE DEFINITION ========================= -->				
+    
+	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
+	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
+	<context:property-placeholder location="classpath:jdbc.properties"/>
+
+    <!-- DataSource configuration for Apache Commons DBCP. -->
+	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
+			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
+			p:username="${jdbc.username}" p:password="${jdbc.password}"/>
+	
+	<!-- JNDI DataSource for JEE environments -->
+	<!--
+	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
+	-->
+
+	<!-- Database initializer. If any of the script fails, the initialization stops. -->
+	<!-- As an alternative, for embedded databases see <jdbc:embedded-database/>. -->
+    <jdbc:initialize-database data-source="dataSource">
+        <jdbc:script location="${jdbc.initLocation}"/>
+        <jdbc:script location="${jdbc.dataLocation}"/>
+    </jdbc:initialize-database>
+
+</beans>    
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/applicationContext-hibernate.xml b/src/main/webapp/WEB-INF/applicationContext-hibernate.xml
index 39a939a..8f414a7 100644
--- a/src/main/webapp/WEB-INF/applicationContext-hibernate.xml
+++ b/src/main/webapp/WEB-INF/applicationContext-hibernate.xml
@@ -4,41 +4,18 @@
 -->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 		xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
-		xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
+		xmlns:tx="http://www.springframework.org/schema/tx"
 		xsi:schemaLocation="
 			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
 
 	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
-	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
+	<!-- (in this case, Hibernate-related settings for the sessionFactory definition below) -->
 	<context:property-placeholder location="classpath:jdbc.properties"/>
 
-	<!--
-		Uses Apache Commons DBCP for connection pooling. See Commons DBCP documentation
-		for the required JAR files. Alternatively you can use another connection pool
-		such as C3P0, similarly configured using Spring.
-	-->
-	
-	<bean id="dataSource" class="org.springframework.samples.petclinic.config.DbcpDataSourceFactory"
-		p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" 
-		p:username="${jdbc.username}" p:password="${jdbc.password}" p:populate="${jdbc.populate}"
-		p:schemaLocation="${jdbc.schemaLocation}" p:dataLocation="${jdbc.dataLocation}"/>
-
-    <!-- DataSource configuration for Apache Commons DBCP. -->
-    <!--
-	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
-			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
-			p:password="${jdbc.password}"/>
-    -->
-	<!-- JNDI DataSource for JEE environments -->
-	<!--
-		<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
-	-->
-
 	<!-- Hibernate SessionFactory -->
 	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
 			p:dataSource-ref="dataSource" p:mappingResources="petclinic.hbm.xml">
@@ -94,4 +71,4 @@
 	<!-- Hibernate's JMX statistics service -->
 	<bean name="petclinic:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>
 
-</beans>
+</beans>
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/applicationContext-jdbc.xml b/src/main/webapp/WEB-INF/applicationContext-jdbc.xml
index e2b9bb4..6819d6d 100644
--- a/src/main/webapp/WEB-INF/applicationContext-jdbc.xml
+++ b/src/main/webapp/WEB-INF/applicationContext-jdbc.xml
@@ -3,45 +3,18 @@
 	Application context definition for PetClinic on JDBC.
 -->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-		xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
-		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
-		xmlns:tx="http://www.springframework.org/schema/tx"
+		xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
+		xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 		xsi:schemaLocation="
 			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
 
-	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
-	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
-	<context:property-placeholder location="classpath:jdbc.properties"/>
-
-	<!--
-		Spring FactoryBean that creates a DataSource using Apache Commons DBCP for connection 
-		pooling. See Commons DBCP documentation for the required JAR files.  This factory bean
-		can populate the data source with a schema and data scripts if configured to do so.
-		
-		An alternate factory bean can be created for different connection pool implementations,
-		C3P0 for example.
-	-->
-	<bean id="dataSource" class="org.springframework.samples.petclinic.config.DbcpDataSourceFactory"
-			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" 
-			p:username="${jdbc.username}" p:password="${jdbc.password}" p:populate="${jdbc.populate}"
-			p:schemaLocation="${jdbc.schemaLocation}" p:dataLocation="${jdbc.dataLocation}"/>
-
-    <!-- DataSource configuration for Apache Commons DBCP. -->
-    <!--
-	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
-			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
-			p:password="${jdbc.password}"/>
-    -->
-	<!-- JNDI DataSource for JEE environments -->
-	<!--
-	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
-	-->
+    <!-- import the dataSource definition -->
+    <import resource="applicationContext-dataSource.xml"/>
 
 	<!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
 	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
@@ -88,4 +61,4 @@
 	<!-- Call monitoring aspect that monitors call count and call invocation time -->
 	<bean id="callMonitor" class="org.springframework.samples.petclinic.aspects.CallMonitoringAspect"/>
 
-</beans>
+</beans>
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/applicationContext-jpa.xml b/src/main/webapp/WEB-INF/applicationContext-jpa.xml
index 5b2be5e..03ff764 100644
--- a/src/main/webapp/WEB-INF/applicationContext-jpa.xml
+++ b/src/main/webapp/WEB-INF/applicationContext-jpa.xml
@@ -4,17 +4,18 @@
 -->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 		xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
-		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
-		xmlns:tx="http://www.springframework.org/schema/tx"
+		xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 		xsi:schemaLocation="
 			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
 
+    <!-- import the dataSource definition -->
+    <import resource="applicationContext-dataSource.xml"/>
+    
 	<!--
 		Activates a load-time weaver for the context. Any bean within the context that
 		implements LoadTimeWeaverAware (such as LocalContainerEntityManagerFactoryBean)
@@ -23,34 +24,10 @@
 	<context:load-time-weaver/>
 
 	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
-	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
+	<!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
 	<context:property-placeholder location="classpath:jdbc.properties"/>
 
-	<!--
-		Spring FactoryBean that creates a DataSource using Apache Commons DBCP for connection 
-		pooling. See Commons DBCP documentation for the required JAR files.  This factory bean
-		can populate the data source with a schema and data scripts if configured to do so.
-		
-		An alternate factory bean can be created for different connection pool implementations,
-		C3P0 for example.
-	-->
-	<bean id="dataSource" class="org.springframework.samples.petclinic.config.DbcpDataSourceFactory"
-			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" 
-			p:username="${jdbc.username}" p:password="${jdbc.password}" p:populate="${jdbc.populate}"
-			p:schemaLocation="${jdbc.schemaLocation}" p:dataLocation="${jdbc.dataLocation}"/>
-
-    <!-- DataSource configuration for Apache Commons DBCP. -->
-    <!--
-	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
-			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
-			p:password="${jdbc.password}"/>
-    -->
-	<!-- JNDI DataSource for JEE environments -->
-	<!--
-	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
-	-->
-
-	<!-- JPA EntityManagerFactory -->
+    <!-- JPA EntityManagerFactory -->
 	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
 			p:dataSource-ref="dataSource">
 		<property name="jpaVendorAdapter">
@@ -108,4 +85,4 @@
 	-->
 	<bean id="clinic" class="org.springframework.samples.petclinic.jpa.EntityManagerClinic"/>
 
-</beans>
+</beans>
\ No newline at end of file