-1

The current workflow I use when deploying an application is to create a JDBC data source in wildfly, then add that data source to the persistence.xml file.

Is there any way to set the data source that the application is using at runtime without modifying persistence.xml?

The crux of this question is trying to understand how to remove any coupling between the application at build time and the deployment environment.

KG6ZVP
  • 3,610
  • 4
  • 26
  • 45

1 Answers1

1

The simplest solution would be to create the data source then set it as the default data source. Then you don't even need to set the data source in your persistence.xml.

For example the following CLI commands will create the data source and set the default data source (java:comp/DefaultDataSource).

embed-server

# Configure the driver
/subsystem=datasources/jdbc-driver=org.postgresql:add(driver-name=org.postgresql, driver-module-name=org.postgresql, driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)

# Configure the data source
/subsystem=datasources/data-source=postgresql:add(driver-name=org.postgresql, jndi-name="java:/jdbc/PostgresDS", enabled=true, connection-url="jdbc:postgresql://localhost/testdb", user-name=user, password="password")

# Change the default data source name java:comp/DefaultDataSource
/subsystem=ee/service=default-bindings:write-attribute(name=datasource, value=java:/jdbc/PostgresDS)

stop-embedded-server

Then in the persistence.xml just remove the <jta-data-source> tag.

James R. Perkins
  • 16,800
  • 44
  • 60
  • It would work for single applications, but it creates similar problems as if I specify the data source in the project. I'm trying to make applications and data source names unaware of each other and to remove table naming considerations from application deployments. This unfortunately could cause table naming collisions and enforce another type of tight coupling. – KG6ZVP Sep 07 '17 at 01:32
  • This is not a great answer, but you may be looking for a *-ds.xml deployment descriptor. https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html/Administration_and_Configuration_Guide/chap-Datasource_Management.html#Deployment_of_-ds.xml_files. That documentation is not the greatest, but you can see an example here https://github.com/wildfly/quickstart/blob/11.x/kitchensink/src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml#L22. – James R. Perkins Sep 07 '17 at 18:43
  • The ds deployment is even worse because it not only clings to the tight coupling I'm seeking to avoid, but introduces additional deployment complexities. I think the question is now whether or not what I'm seeking to do is possible. – KG6ZVP Sep 07 '17 at 20:46
  • Maybe I'm just not understanding the question then. At some point you have to know what the JNDI name of the data source is going to be. At what point is that? – James R. Perkins Sep 07 '17 at 22:02
  • The JNDI name for an application would be picked at deployment time. The idea is to have an app without a JNDI name specified and essentially the same behavior as if there was a per-app default data source. Like I said, it may not be possible, but since there is an ability to have multiple apps use a global default, I'd like to think it could be done individually. – KG6ZVP Sep 08 '17 at 01:51
  • You'd probably have to write your own subsystem to do that so you can hook into the deployment chain. I guess the thing I don't understand is how the JNDI would be picked :) – James R. Perkins Sep 08 '17 at 14:32