Using mysql-ds.xml and applicationContext-data with the Hibernate Example

In the previous Hibernate example with mySQL, the datasource was configured in spring-servlet.xml http://cuppajavamattiz.com/2007/09/15/a-simple-spring-hibernate-example/ In this example we do away with spring-servlet.xml and instead use a combination of mysql-ds.xml and applicationContext-data.xml to configure the datasource.
We also add ehcache-1.2.4.jar to lib/third-party from spring-framework-2.0.6-with-dependencies/lib/ehcache in spring-framework-2.0.6-with-dependencies.zip that comes with the spring distribution.

web.xml is modified as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
	<!-- Spring listener -->
		<listener>
			<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- Faces Servlet -->
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Faces Servlet Mapping -->
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.jsf</url-pattern>
	</servlet-mapping>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext*.xml</param-value>
	</context-param>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

mysql-ds.xml is put in server/default/deploy and has the following source:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
	<local-tx-datasource>
		<jndi-name>dbpool</jndi-name>
		<connection-url>jdbc:mysql://localhost:3306/AUTHORS</connection-url>
		<driver-class>com.mysql.jdbc.Driver</driver-class>
		<user-name>admin</user-name>
		<password>admin</password>
		<metadata>
			<type-mapping>mySQL</type-mapping>
		</metadata>
	</local-tx-datasource>
</datasources>

applicationContext-data.xml is introduced in conf folder of work and has following source:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- Spring configuration file with data services layer configuration. -->
<beans>
	<bean id="dataSourceMySQL" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:/dbpool" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingJarLocations">
			<value>WEB-INF/lib/mattiz.jar</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
				<prop key="hibernate.order_updates">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref local="dataSourceMySQL" />
		</property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
		<property name="jdbcExceptionTranslator">
			<ref local="jdbcExceptionTranslator" />
		</property>
	</bean>
	<bean id="jdbcExceptionTranslator"
		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
		<property name="dataSource">
			<ref local="dataSourceMySQL" />
		</property>
	</bean>
</beans>

Note:
You can configure JNDI instead of datasource in spring configuration file using “org.springframework.jndi.JndiObjectFactoryBean”.

<bean id="dataSourceMySQL" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/dbpool" />
</bean>


Note:
Caching is widely used for optimizing database applications. Hibernate uses two different caches for objects: first-level cache and second-level cache.
First-level cache is associated with the Session object, while second-level cache is associated with the Session Factory object. By default, Hibernate uses first-level cache on a per-transaction basis. Hibernate uses this cache mainly to reduce the number of SQL queries it needs to generate within a given transaction. For example, if an object is modified several times within the same transaction, Hibernate will generate only one SQL UPDATE statement at the end of the transaction, containing all the modifications.
To reduce database traffic, second-level cache keeps loaded objects at the Session Factory level between transactions. These objects are available to the whole application, not just to the user running the query. This way, each time a query returns an object that is already loaded in the cache, one or more database transactions potentially are avoided. In addition, you can use a query-level cache if you need to cache actual query results, rather than just persistent objects. The query cache should always be used in conjunction with the second-level cache. One of the open source cache implementation that Hibernate supports out-of-the-box is EhCache.

EHCache is a fast, lightweight, and easy-to-use in-process cache. It supports read-only and read/write caching, and memory- and disk-based caching. However, it does not support clustering.

To activate second-level caching, you need to define the hibernate.cache.provider_class property in the hibernate.cfg.xml file as follows:

<hibernate-configuration>
<session-factory>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EHCacheProvider</property>
</session-factory>
</hibernate-configuration>

By default, the second-level cache is activated and uses the EHCache provider. To use the query cache you must first enable it by setting the property hibernate.cache.use_query_cache to true in hibernate.properties.

applicationContext-service.xml replaces applicationContext.xml in the original application while the source remains unchanged.

Rest of the files is reused and no other modification is made.

Modifying the above example to use Oracle database

Instead of mysql-ds.xml we place oracle-ds.xml in server/default/deploy which has the following source:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
	<local-tx-datasource>
		<jndi-name>oradbpool</jndi-name>
		<connection-url>jdbc:oracle:thin:@localhost:1521:mattiz
		</connection-url>
		<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
		<user-name>scott</user-name>
		<password>tiger</password>
		<metadata>
			<type-mapping>Oracle</type-mapping>
		</metadata>
	</local-tx-datasource>
</datasources>

applicationContext-data.xml in conf of work folder is modified thus:;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- Spring configuration file with data services layer configuration. -->
<beans>
	<bean id="dataSourceOra" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:/oradbpool" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingJarLocations">
			<value>WEB-INF/lib/mattiz.jar</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
				<prop key="hibernate.order_updates">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref local="dataSourceOra" />
		</property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
		<property name="jdbcExceptionTranslator">
			<ref local="jdbcExceptionTranslator" />
		</property>
	</bean>
	<bean id="jdbcExceptionTranslator"
		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
		<property name="dataSource">
			<ref local="dataSourceOra" />
		</property>
	</bean>
</beans>

Note that both oracle-ds.xml and mysql-ds.xml can co-exist in server/default/deploy

Download source code here Spring_Hibernate_with_app_context_data

Get the Oracle version here Oracle_Hibernate_version

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: