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

Java 如何读取ActiveJDBC的属性值设置?,java,activejdbc,javalite,Java,Activejdbc,Javalite,我正在使用activejdbc.properties文件指定database.properties值的位置 activejdbc.properties env.connections.file=/opt/apps/conf/database.properties 数据库.属性(位于服务器上) 我现在尝试的是使用连接池。我已经研究了您的示例如何做到这一点,但是我不完全理解如何提取数据库属性值来帮助创建连接池 以下是您的示例: public void shouldUseConnectionFrom

我正在使用activejdbc.properties文件指定database.properties值的位置

activejdbc.properties

env.connections.file=/opt/apps/conf/database.properties
数据库.属性(位于服务器上)

我现在尝试的是使用连接池。我已经研究了您的示例如何做到这一点,但是我不完全理解如何提取数据库属性值来帮助创建连接池

以下是您的示例:

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、用户名、密码等,还是我只需要在服务器上读取文件并手动拉入?

以下是您需要做的

->删除文件
activejdbc.properties

->由于Spark正在运行Jetty,请使用Jetty配置JDBC池:

->使用以下内容创建属性文件
/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
我想你需要添加更多的方法来完成任何工作

启动您的程序:

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
请参阅更多: