user725455
user725455

Reputation: 475

how to change datasource connection url dynamically in Jboss 6

I am using Jboss AS6 with container manager tracsaction. Normally I am using a single MYSQL data source defined in mysql-ds.xml. Now I have an user requirement that multiple users can have different database contents.So I am planning to create a new database for each user.In the future I can change my database design. But now for a quick solution, I need to find a way to crossover between different databases.

this is my default configuration. I need to change dbname

<local-tx-datasource>
    <jndi-name>DefaultDS</jndi-name>
    <connection-url>jdbc:mysql://144.0.0.1:3306/**dbname**</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>password</password>

    <max-pool-size>100</max-pool-size>
    <min-pool-size>0</min-pool-size>
    <connection-property name="readOnly">false</connection-property>
  <autoReconnect>true</autoReconnect>
  <failOverReadOnly>false</failOverReadOnly>
    <maxReconnects>0</maxReconnects>
    <initialTimeout>15</initialTimeout>
    <idle-timeout-minutes>0</idle-timeout-minutes>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
    <!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
  -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
  -->
</local-tx-datasource>

I am using Container manager transcaction.I need to find a way to change connection-url of my data source configuration.Then clear entity manager to connect new database.Is this possible

Upvotes: 0

Views: 2247

Answers (2)

Manisha
Manisha

Reputation: 111

I think you could use something like:

<connection-url>${connectionURL:jdbc:mysql://144.0.0.1:3306/xyz}</connection-url>

and then pas the -DconnectionURL=jdbc:mysql://144.0.0.1:3306/abc to the standalone.sh/bat

Upvotes: 0

Anton Astafiev
Anton Astafiev

Reputation: 126

For your task you actually can just hold multiple (5 in your case) persistence contexts. Then you link to appropriate context by choosing it according to the user name for example. This is the simpliest solution. It would work but all 5 contexts would be open and hold connection. Maybe it's what you need: from one hand this context are always ready for use and don't need to be warmed.

Another option is to construct persistence conext manually like in Java SE envirionment. This can be helpful if you are not able to hold many resources and switching is not so regular (once a day. for example).

You can find a lot of articles describing how to use JPA in Java SE environment. You are able to use them nearly in the same manner (so you will not use container injection mechanism, but find Persistnece Context through raw JNDI)

https://dzone.com/articles/jpa-tutorial-setting-jpa-java http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/SettingUpJPA/SettingUpJPA.htm

Upvotes: 1

Related Questions