Announcement Announcement Module
No announcement yet.
Enable disable bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enable disable bean


    my application uses a bean called dataSource in order to connect to a MySQL database. It is injected into a DatabaseManager bean that handles the DB operations. When I'm offline the bean creation process fails, because the dataSource can't be instantiated.

    Now I want to make the DatabaseManager configurable

    <property name="offlineMode" value"false" />
    If it is set to true I don't want the dataSource bean to be created. Is this possible?

  • #2
    Extend your data source class

    So long as you include the dataSource bean in your config, your app initialization will fail, regardless of what you do to DatabaseManager.

    One approach would be to extend your data source object (e.g. org.apache.commons.dbcp.BasicDataSource) with a wrapper class that is offline-aware.

    HTML Code:
    <bean id="myDataSource" class="my.project.db.MyDataSource">
      <!-- wrapped data source props... -->
      <property name="offlineMode">true</property>
    If in offline mode, your data source should return a dummy implementation of Connection (I'm still assuming BasicDataSource here). Your DatabaseManager should detect that the Connections it's getting are dummies and perform in offline mode.

    Hope that helps.


    • #3
      it can also be solved by making the datasource lazyinit and the database manager shld not get the bean if the database is offline.

      IMO Extending a Datasource class might not be a good idea as your application would then work only with that datasource...