3

I have a spring app which do minimal operations with DB. And I have a requirment that my application should run under the absence of DB(or when db is down).Below is my datasource configuration.

<bean id="dt31DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" depends-on="systemPropertyInitializer"
                      p:driverClass="${dt31.driver_class}"
                      p:jdbcUrl="${dt31.url}"
                      p:user="${dt31.username}"
                      p:password="${dt31.password}"
                      p:idleConnectionTestPeriod="1000"
                      p:maxPoolSize="4"
                      p:minPoolSize="2"
                      p:maxIdleTime="2000"
                      p:unreturnedConnectionTimeout="600"
                      p:contextClassLoaderSource="library"
                      p:privilegeSpawnedThreads="true"
                      p:initialize=false
                      />

    <bean id="dt31SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dt31DataSource"/>
        <property name="packagesToScan" value="com.t22.dt31"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${dt31.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    false
                </prop>
                <prop key="hibernate.hbm2ddl.auto">
                    update
                </prop>
            </props>
        </property>
    </bean>

I found a page in google,saying to use "initialize: false" in spring data source configuration.But I am using "ComboPooledDataSource" datasource which does not have this property.Is there any other way to achieve this ?

Community
  • 1
  • 1
Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38
  • Spring app will start even if DB is down given that your landing page is not instantiating any model class which has a table in DB. So you can show a page, if that's what you want, but I don't understand the purpose. – We are Borg Jul 31 '15 at 07:50
  • Borg,Thanks for prompt comment.When my app is initially loading it is trying to create session factory bean which intern need data source configuration.I think ComboPooledDataSource bean is preparing a connection to database in loading phase and it is failing as DB is down. Thowing error like : Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@5ff26372 is interrupting all Threads waiting on a resource to check out. – Sunil Kumar Jul 31 '15 at 08:14
  • Yes, because you have connection timeout, idle test period in your combo thing. I would suggest to remove that. And for starters, it is best to use a simple datasource. Secondly, may I ask why do you have such a requirement, what benefit does this have? I am curious.. – We are Borg Jul 31 '15 at 08:16
  • I will test your suggestion.Mean while I give my requirement, my app will mostly work with file system and minimal with DB.In start up, my app will run some migration scripts which deals only with file system,once app is up if user did some actions in UI,then it will deal with DB.So in start up some times my DB may be down or may not exist. – Sunil Kumar Jul 31 '15 at 08:43

1 Answers1

1

The application service layer can operate with two DAO layers (file system and database), so when the DB is down you catch the connection acquire exception and switch to the file system instead.

Having two sources of truth is going to make it difficult to preserve consistency across two different data sources, especially if one resource is not available.

When both resources are available you can use XADisk and Bitronix for XADataSource and JTA transaction management.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911