如何读取 ActiveJDBC 的 属性 值设置?

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