Announcement Announcement Module
Collapse
No announcement yet.
JDBC-based bean definitions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JDBC-based bean definitions

    Just wanted to verify my understanding of the following issue at
    http://opensource.atlassian.com/proj.../browse/SPR-95
    Addresses this a new feature where I can store my bean definitions in a DB table instead of an XML file?
    is this correct?
    Because this would come quite nice to play with the Timer functionality in Spring. I could specify the timertasks in a DB table then which I need for a project.
    Is there any possibility to work on this and contribute it?
    cheers.

  • #2
    Karsten,

    Yes, the existing JdbcBeanDefinitionReader can read bean definitions from a database, but it's not incredibly flexible. Additionally, there is the question of dynamic reconfiguration. One of the main reasons why somebody would put some data in a db (at least some of the time) is probably because they need to be able read that data dynamically, not just on program startup.

    As for user contributions for this or any other area of Spring, sure, they are much appreciated. Any code can be submitted as a patch in Jira, and discussed in the dev list.

    Comment


    • #3
      Colin,
      thanks for the quick reply.
      I will have a closer look at this subject and come back with more thoughts/code.
      Keep your great work up!

      Comment


      • #4
        clarification needed

        I just wonder about some things with the functionality of retrieving bean definitions from a DB. Please correct me if I am wrong:
        To retrieve bean configs from a DB, I need to store them as stated in the JdbcBeanDefinitionReader class (beanID, property name and value), then I need to instatiate a new JdbcBeanDefinitionReader, set a Datasource or JdbcTemplate on it. And I could do this with spring in the applicationContext.xml, or?
        After that I have my own ServletContextListener implementation, which follows the spring ContextLoaderListener. In this class I retrieve the JdbcBeanDefinitionReader from the context and call the loadBeanDefinitions(String sql) method with the appropriate SQL.
        The beans will get loaded into the same context as the others? or a new one?
        Is this working at all, or am I missing something.
        Thanx for clarifying.

        Comment


        • #5
          Originally posted by Colin Sampaleanu
          Karsten,

          Yes, the existing JdbcBeanDefinitionReader can read bean definitions from a database, but it's not incredibly flexible. Additionally, there is the question of dynamic reconfiguration. One of the main reasons why somebody would put some data in a db (at least some of the time) is probably because they need to be able read that data dynamically, not just on program startup.
          I need to do something similar in that it is "policy" that if a configuration file changes, the application must go through a complete 30-day round of regression testing; yet there is no such rule for changing a value in the database. :roll:

          Comment


          • #6
            Originally posted by Colin Sampaleanu
            Karsten,

            Yes, the existing JdbcBeanDefinitionReader can read bean definitions from a database, but it's not incredibly flexible. Additionally, there is the question of dynamic reconfiguration. One of the main reasons why somebody would put some data in a db (at least some of the time) is probably because they need to be able read that data dynamically, not just on program startup.
            I need to do something similar in that it is "policy" that if a configuration file changes, the application must go through a complete 30-day round of regression testing; yet there is no such rule for changing a value in the database. :roll:

            Comment


            • #7
              Dynamic reconfiguration of bean properties using JDBC

              Originally posted by Colin Sampaleanu
              Karsten,

              Yes, the existing JdbcBeanDefinitionReader can read bean definitions from a database, but it's not incredibly flexible. Additionally, there is the question of dynamic reconfiguration. One of the main reasons why somebody would put some data in a db (at least some of the time) is probably because they need to be able read that data dynamically, not just on program startup.

              As for user contributions for this or any other area of Spring, sure, they are much appreciated. Any code can be submitted as a patch in Jira, and discussed in the dev list.
              I have written some code to initialize bean properties using initialization parameters stored in various locations e.g. database table, servlet context, servlet init parameters, Map, Properties, etc. It works both during application initialization and later during application execution. i.e. it supports dynamic reinitialization of bean properties,

              I would be happy to submit the code to the Spring project. Let me know if you are interested.

              See my posting from August 30 for additional information

              Comment


              • #8
                The best thing would be to put it on the wiki (Confluence) somewhere, so other people can get at it...

                Comment


                • #9
                  MHarhen,

                  Your approach seems very interesting. I have two questions with regards to beans configurations using PropertyMapper:

                  1. Is it possible to configure PropertyMapper to call some initialization methods after properties were updated (to emulate Spring' init-method)?
                  2. How do you manage "dynamic reconfiguration": reconfigure a bean that is being used by other beans.

                  I am asking these questions because I will be using such "hot-configuration/reconfiguration" in a future project.

                  Thank you.

                  Comment


                  • #10
                    Originally posted by irbouho

                    1. Is it possible to configure PropertyMapper to call some initialization methods after properties were updated (to emulate Spring' init-method)?
                    2. How do you manage "dynamic reconfiguration": reconfigure a bean that is being used by other beans.
                    Answers:
                    1. Yes - PropertyMapper has an initMethod property.
                    2. By dynamic reconfiguration, I mean changing the properties of a bean during execution. It is done by calling the PropertyMappers.refreshAll() method.

                    Comment


                    • #11
                      MHarhen,

                      I too would be interested in your approach also. For clustering and other crazy requirements we keep some properties in a central database. So during init we need to get them, then configure some root beans.

                      J. Betancourt

                      Comment


                      • #12
                        MHarhen,
                        is there any ready to use code? any further information on where to get it?
                        thanx for the promising looking code.

                        Comment


                        • #13
                          Originally posted by karsten
                          is there any ready to use code? any further information on where to get it?
                          thanx for the promising looking code.
                          The code is still in progress. It works, but I need to clean it up. I'll target completion in about a week, if not sooner.

                          Comment


                          • #14
                            Code to set bean properties using a database.

                            Originally posted by karsten
                            is there any ready to use code? any further information on where to get it?
                            thanx for the promising looking code.
                            Code has been updated as of September 5. See my September 5 posting.

                            Comment


                            • #15
                              Code to set bean properties using a database

                              Originally posted by karsten
                              is there any ready to use code? any further information on where to get it?
                              thanx for the promising looking code.
                              I have written code to enable initialization of a bean using initialization parameters from a database table. The code can be downloaded from:
                              http://members.cox.net/jwebutil/InitParameters.zip

                              How to use it:

                              1. Create a bean class net.sourceforge.MyBean. To use the following example, create setWebMaster(...) and getWebMaster() methods.

                              2. Create a database table called "InitParametersTable", with columns named "Parameter" and "Value". You may change these using the SQL shown below.
                              Enter the names and values of the parameters you wish to use as the rows in the table.
                              e.g. Parameter: WEBMASTER
                              Value: The web master

                              3. Create a datasource with id="DATASOURCE" or whatever id you wish to use.

                              4. Create an InitParameters bean, specifying the DataSource and SQL needed to access the table.

                              Code:
                              <bean id="initParameters"
                                      class="net.sourceforge.jwebutil.util.initparameters.InitParametersFactory"
                                      factory-method="createInstance">
                                  <description>Database Init Parameters</description>
                                  <constructor-arg index="0"><ref bean="DATASOURCE" /></constructor-arg>
                                  <constructor-arg index="1">
                                    <value>
                                      <!&#91;CDATA&#91;
                                        SELECT Parameter, Value FROM InitParametersTable;
                                      &#93;&#93;>
                                    </value>
                                  </constructor-arg>
                                </bean>
                              5. Create a bean, using the PropertyMapper factory. Specify the bean's class using the "targetClass" property. Specify the bean's properties using the "properties" property. Each key refers a property of the bean. Each value refers to the name of the Initialization Parameter in the database table.
                              Code:
                              <bean id="myBean" class="net.sourceforge.jwebutil.util.initparameters.PropertyMapper">
                                  <description>Global Application Configuration properties</description>
                                  <property name="targetClass"><value>net.sourceforge.MyBean</value></property>
                                  <property name="initParameters"><ref bean="initParameters" /></property>
                                  <property name="properties">
                                   <props>
                                    <prop key="webMaster">#WEBMASTER</prop>
                                   </props>
                                  </property>
                                </bean>
                              Notes:

                              1) Mandatory initialization parameters are indicated by a preceding "#". e.g. <prop key="webMaster">#WEBMASTER</prop>

                              2) Default values are indicated by a semi-colon followed by the default value. e.g. <prop key="webMaster">WEBMASTER;Tom Swift</prop>

                              3. You can specify a reference to a bean by preceding its name with a "*". e.g. <prop key="theBean">*theBeanRef</prop>

                              4. You can specify a value for a property by preceding its value with a "@". e.g. <prop key="webMaster">@Tom [email protected]</prop>. In this case, the value of the property is used as-is, without looking it up in the InitParameters object.

                              5. All of your custom Property Editors are supported.

                              6. The InitializingBean and DisposableBean interfaces are supported. You can also specify a method to be called after all of the bean's properties are set - e.g. <property name="initMethod"><value>init</value></property>. In addition, you can specify a destroyMethod e.g. <property name="destroyMethod"><value>destroy</value></property>.

                              7. You can update all beans at any time by calling propertyMapManager().refreshAll()

                              8. You can update an individual bean at any time by calling propertyMapManager().refresh(theBean)

                              9. You can also use initialization parameters from the ServletContext, JNDI context, any Map, any Properties object, etc - see the InitParametersFactory class.

                              10. Use <property name="singleton"><value>false</value></property> to specify that the bean is a prototype. The default is "true", specifying a singleton.

                              11. Comments are appreciated...

                              Comment

                              Working...
                              X