I am using the activejdbc.properties file to specify the location of my database.properties values.
activejdbc.properties
env.connections.file=/opt/apps/conf/database.properties
database.properties (Located on the server)
development.driver=oracle.jdbc.OracleDriver
development.username=myusername
development.password=mypassword
development.url=jdbc:oracle:thin:@//dburl:1521/testdb.world
What I am trying to do now is use Connection Pooling. I've examined your example how to do this, however I'm not fully understanding how to pull my database property values to help create the connection pool.
Here's your example:
public void shouldUseConnectionFromPool() throws PropertyVetoException, SQLException, ClassNotFoundException {
Class.forName(driver());
DataSource dataSourceUnpooled = DataSources.unpooledDataSource(url(), user(), password());
DataSource dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled); //init the connection pool
Base.open(dataSourcePooled); //get connection from pool
Person.deleteAll(); //clean DB before test
Person.createIt("name", "Matt", "last_name", "Diamont", "dob", "1962-01-01");
a(Person.findAll().size()).shouldBeEqual(1);
Person.deleteAll();//clean DB after test
Base.close();// really connection goes back to pool
DataSources.destroy(dataSourcePooled);//shut down the pool
}
And here's mine. I'm using JavaSpark and trying to define my pool in the main() so when the server is spun up.
public static void main(String[] clargs) {
try {
DataSource dataSourceUnpooled = DataSources.unpooledDataSource("jdbc:oracle:thin:@//dburl:1521/testdb.world", "myusername", "mypassword");
dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled); //init the connection pool
} catch (SQLException e1) {
e1.printStackTrace();
}
before("/*", (req, res) -> {
if (!Base.hasConnection()) {
System.out.println("Database Connection OPEN.");
Base.open(dataSourcePooled); //get connection from pool ;
}
}
after("/*", (req, res) -> {
Base.close(); // really connection goes back to pool
});
get("/exit", (req,res)->{
if (Base.hasConnection()) {
Base.close(); // really connection goes back to pool
}
DataSources.destroy(dataSourcePooled); //shut down the pool
System.exit(0);
return "Application shutdown";
});
}
So now I'm trying to remove my hardcoded property values and used what is setup in my files. I see you using url(), etc but not sure if that's a private method you created for your tests. So my question is, is there an easy way to use URL, USERNAME, PASSWORD etc from what's pulled in by ActiveJDBC or do I need to just read the file on the server and manually pull it?
Here is what you need to do
-> Delete file activejdbc.properties
.
-> Since Spark is running Jetty, use the Jetty to configure a JDBC pool: https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
-> Create a property file /opt/apps/conf/database.properties
with this content:
development.jndi=java:comp/env/jdbc/acme
-> Rewrite your program:
public static void main(String[] clargs) {
before("/*", (req, res) -> {
Base.open(); //will pickup 'development' connection from property file
});
after("/*", (req, res) -> {
Base.close(); // really connection goes back to pool
});
get("/exit", (req, res) -> {
System.exit(0);
return "Application shutdown";
});
}
I guess you need to add more methods to get any work done.
Start your program:
java com.company.project.Main -cp myprogram.jar -Denv.connections.file=/opt/apps/conf/database.properties
See more: http://javalite.io/database_connection_management#location-of-property-file