4 Replies Latest reply on Aug 21, 2017 12:11 PM by nitingautam

    Data source configuration during runtime


      Hi everyone. I need to implement a solution that would enable me to change data source configuration during runtime. There is a system A that admins have access to and whre they put configuration for stuff, including db configuration. You can think of it as "Configuration Central".


      Component in another system - lets call it B - periodically reads that configuration. When configuration in A changes, system B detects that change and updets itself. I need to implement a solution that would enable me to change datasource configuration on the fly to use new configuration from system A without a need to restart the jboss.


      Conenction URL change


      I am able to change the connection url infollowing way...

      1. DataSource has few underlying components (MBeans). One of them is ManagedConnectionFactory.
      2. I do a JNDI lookup on this one, then cast it to org.jboss.resource.connectionmanager.RARDeploymentMBean to have access to properties
      3. I use RARDeploymentMBean#setManagedConnectionFactoryAttribute(arg0, arg1, arg2) to set the url

      Looks good so far.


      Credentials switch


      Then I need to update the the credentials - username and password. There is a component named org.jboss.security.auth.login.DynamicLoginConfig with a parameter AuthConfig that points to a file that stores various policies conatining username and password. Here is an abstract from this file:

        <application-policy name="MyName_LoginDomain">
            <login-module code="org.jboss.resource.security.SecureIdentityLoginModule" flag="required">
          <module-option name="username">username</module-option>
          <module-option name="password">password</module-option>
          <module-option name="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MyName_DS</module-option>


      I also found that each datasource has a ConnectionManager attribute, and this connection manager could also be found using JNDI lookup. It has an attribute called SecurityDomainJndiName with value of MyName_LoginDomain. Using this one I could switch between predefined LoginDomain as I like to call them. Those are in fact instances of org.jboss.security.plugins.SecurityDomainContext.



      Admins switch data base to which in system A  to which system B should connect. I change te url, then I switch to correct SecurityDomainContext and that is fine. However when database username or password changes I have no idea what to do other than updating the file pointed by DynamicLoginConfig.AuthConfig and restarting jboss after it. Is there any way to change username and password associated with given SecurityDomainContext during runtime?


      Sorry for such a large post, but I wanted to describe the whole problem to be clear. If you need any addtional information so that we could think it out together just let me know. Thanks a lot for any help!


        • 1. Re: Data source configuration during runtime

          I never think about such 'on the fly' configuration.


          But I suppose that this is something like change the attributes of the ManagedConnectionPool MBean.

          In this case you have to call flush() to activate the configuration.

          This will unfortunately drop all active connections!

          • 2. Re: Data source configuration during runtime

            Thanks for your help. The problem is I don't see any way I could change those credentials in the SecurityDomainContext - absolutely no idea how to get it solved

            I'll keep you informed should I find a solution for it.

            • 3. Re: Data source configuration during runtime

              You can configure connection url in following way:

              1. Look up the WrapperDataSourceServiceMBean
              2. Than get the TxConnectionManagerMBean (Object name stored in component from 1 - getConnectionManager)
              3. Than use the getManagedConnectionFactory to get the connection factory
              4. Set the connectionURL attribute in the factory. It is an instance of org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory. It has the connectionURL attribute, but I couldn't find this class in any of the client/lib jars in jboss so I used a reflection to set a value of this field.
              5. Get the pool from TxConnectionManagerMBean and flush it
              6. URL has changed.


              I haven't yet figured out how to change the credentials for given data source. It uses a Subject that gets created and instantiated "somwhere" hope that I find it.

              • 4. Re: Data source configuration during runtime

                were you able to find fix of this scenario? I also have similar requirement where I have to switch DS runtimr on selection of a Client so that application can read/update to that clients database.