views:

51

answers:

2

I have been working with Spring Framework 3.0 for the last couple weeks and have been really impressed. This is my first project with Java. My main problem is that I can't find a way to let the end user manage their own database connection from within the web application. The project I am working on will be installed on the client's computer and they will provide all the connection information. At this point whenever the DataSource is unavailable I get an error I am not sure how to handle:

java.sql.SQLException: Network error IOException: Connection refused

I tried to lazy load the dataSource in the xml configuration, however it was still loaded when application started during the Pre-instantiating singletons phase.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"     
    lazy-init="true">    
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url"
      value="${jdbc.urlprefix}://${jdbc.server}:${jdbc.port}/${jdbc.database}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    lazy-init="true">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">false</prop>
      </props>
    </property>
    <property name="annotatedClasses">
      <list>
           ....
      </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    lazy-init="true">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

I am hoping when the database is unavailable or not configured correctly I could just redirect the user to a page not dependent on the database connection so the settings can be managed. I am just not sure how accomplish this when the IoC container is expecting the DataSource to be configured correctly from the start.

Thank you in advance!

A: 

this IS a hack.... use an in memory database as a default configuration.

if when the system comes up, it is still the default database, then you know an error ocurred.

Aaron Saunders
+1  A: 

Spring's AbstractRoutingDataSource is an intermediary DataSource that routes getConnection() calls to another target DataSource determined an run-time. Set the default target data source to an in-memory database so there is a database to connect to when the application context is loaded. When the user provides the connection information, your AbstractRoutingDataSource subclass can route to another DataSource configured with the connection information.

Jim Huang