...
To get the best of both worlds, we need the 'PortalDb' bean to not be a direct datasource nor a JNDI datasource but instead, be a custom bean which mediates between JNDI and direct connection and returns the appropriate datasource ready for use. Using the attached custom class (eduorg.jasig.wiscportal.myspring.beans.DataSourceMediatingFactoryBeanfactory.MediatingFactoryBean) we can configure the 'PortalDb' bean to automatically be either a JNDI managed datasource if it is available (ie at run-time) or a direct connection datasource if JNDI is not available (ie at build-time). The datsourceContext.xml file would now look like this:
Code Block |
---|
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- | DataSource objects used by the portal should be configured +--> <bean id="PortalDb" class="eduorg.jasig.wiscportal.myspring.beans.factory.DataSourceMediatingFactoryBeanMediatingFactoryBean"> <property name="jndiNametype" value="JNDIPortalDbjavax.sql.DataSource" /> <property name="dataSourceBeanNamedelegateBeanNames" value="DirectPortalDb" />> <list> <value>PortalDb.JNDI</value> <value>PortalDb.direct</value> </list> </property> </bean> <bean id="JNDIPortalDbPortalDb.JNDI" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/uPortalDB" /> </bean> <bean id="DirectPortalDbPortalDb.direct" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"> <property name="driverClassName" value="${hibernate.connection.driver_class}" /> <property name="url" value="${hibernate.connection.url}" /> <property name="username" value="${hibernate.connection.username}" /> <property name="password" value="${hibernate.connection.password}" /> <property name="maxActive" value="50" /> <property name="maxIdle" value="10" /> <property name="maxWait" value="1000" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="300" /> <property name="logAbandoned" value="true" /> </bean> <bean id="PortalDB.metadata" class="org.jasig.portal.rdbm.DatabaseMetaDataImpl"> <constructor-arg index="0" ref="PortalDb" /> </bean> <alias alias="PersonDB" name="PortalDb"/> <bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false"> <property name="location" value="classpath:/properties/rdbm.properties" /> </bean> </beans> Using this configuration allows us to have the best of both worlds. |