[英]How to read the property values setup for ActiveJDBC?
我正在使用activejdbc.properties文件來指定我的database.properties值的位置。
activejdbc.properties
env.connections.file=/opt/apps/conf/database.properties
database.properties (位於服務器上)
development.driver=oracle.jdbc.OracleDriver
development.username=myusername
development.password=mypassword
development.url=jdbc:oracle:thin:@//dburl:1521/testdb.world
我現在想做的是使用連接池。 我已經檢查了您的示例如何執行此操作,但是我不完全了解如何提取數據庫屬性值以幫助創建連接池。
這是您的示例:
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
}
這是我的。 我正在使用JavaSpark並嘗試在main()中定義我的池,以便在啟動服務器時啟動。
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";
});
}
因此,現在我試圖刪除我的硬編碼屬性值,並使用文件中的設置。 我看到您使用url()等,但不確定這是否是您為測試創建的私有方法。 所以我的問題是,是否有一種簡單的方法可以使用ActiveJDBC提取的內容中的URL,USERNAME,PASSWORD等,還是我只需要讀取服務器上的文件並手動將其提取?
這是你需要做的
->刪除文件activejdbc.properties
。
->由於Spark正在運行Jetty,請使用Jetty配置JDBC池: https : //wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
->創建具有以下內容的屬性文件/opt/apps/conf/database.properties
:
development.jndi=java:comp/env/jdbc/acme
->重寫程序:
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";
});
}
我想您需要添加更多方法來完成任何工作。
啟動程序:
java com.company.project.Main -cp myprogram.jar -Denv.connections.file=/opt/apps/conf/database.properties
查看更多: http : //javalite.io/database_connection_management#location-of-property-file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.