1

I am working on a Java EE 7 application. I am using Payara micro to deploy my WAR files. Now, I need JDBC connectivity in my application, but I need to keep the database IP/username/password somewhere I can change later on, without re-uploading and deploying WAR file again.

Could anybody please tell me how can I achieve this?

EDIT:

I came accross a solution to that on SO: https://stackoverflow.com/a/6296375/1931698

But, I am looking for a solution without all that plumbing. Inheriting DataSource just to have connection info in some external file looks like overkill.

EDIT:

Also, it would be really helpful if I can just provide a configuration panel to the user, where he / she can enter JDBC connection info. Is there a way to change that info at runtime (effectively discarding existing connection pool and creating a new one)?

Community
  • 1
  • 1
Prakhar Mishra
  • 1,586
  • 4
  • 28
  • 52
  • 2
    Configure the data source in Payara itself, and use JNDI to look it up, other solutions are way too broad and will involve writing the plumbing to look up external config files, etc. You get that all out of the box. – Mark Rotteveel Apr 17 '17 at 08:07

2 Answers2

0

Payara Micro should allow system property replacement in Datasource definitions using the syntax ${system.property.name} you can use that to define the database user name and password as well as the connect strings.

There is also environment variable support in 171.1 onwards using the syntax ${ENV=env.name} where env.name is the environment variable name.

leet java
  • 304
  • 1
  • 3
-1

Make use of java.utils.Properties with a seperate myproperties.properties file in your src folder (in classpath).

Properties prop = null;
try { 
 prop = new Properties();
 InputStream inputStream  = getClass().getClassLoader()
                          .getResourceAsStream("myProperty.‌​properties");
  if (inputStream != null) { 
    prop.load(inputStream); 
  } 
} catch (IOException e) {
   errorLog.error("Property file not found in classpath: 
                           ClientChecking Class.");
}
String userName = prop.getProperty("USERNAME");  

myproperties.properties file must contain:

USERNAME=user123 

If you are using Apache tomcat for hosting, then you can find and edit the propertyfile from path WebContent(Root dir)/WEB-INF/classes/

S Jayesh
  • 191
  • 1
  • 4
  • 19
  • And how can I make this properties file available to Java EE container in order to get all connection pooling benefits of Java EE? – Prakhar Mishra Apr 17 '17 at 07:20
  • while development put your property file within classpath (i.e. in source folder) and while application is running this file will be in classpath with your .class files. – S Jayesh May 05 '17 at 05:42