【问题标题】:Changing programmatically connection properties after failed connection attempt - Hibernate连接尝试失败后以编程方式更改连接属性 - 休眠
【发布时间】:2013-09-08 11:41:33
【问题描述】:

如果休眠无法连接到数据库,我想以编程方式更改连接属性。我正在尝试使用以下代码执行此操作:

    String connectionString = "jdbc:oracle:thin:@" + Cfg.getHost() + ":" + Cfg.getPort() + ":" + Cfg.getSid();

    Configuration configuration = new Configuration()            
            .setProperty("hibernate.connection.url", connectionString)
            .setProperty("hibernate.connection.password", Cfg.getPassword())
            .setProperty("hibernate.connection.username", Cfg.getLogin());

    sessionFactory = configuration.configure().buildSessionFactory();

属性来自属性文件。但是 buildSessionFactory 适用于第一次连接尝试。对于其他人的尝试,他忽略了配置并且不想创建新的 SessionFactory。

【问题讨论】:

    标签: java hibernate database-connection


    【解决方案1】:

    SessionFactory 应该被创建一次。稍后在运行时,您应该使用该工厂为您的数据库操作创建会话。

    【讨论】:

    • 第一次连接数据库不正常怎么连接数据库?
    • 我想更改数据库,打开我无法连接的数据库的会话不要帮助我。
    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多