繁体   English   中英

使用休眠在运行时为derby数据库设置bootPassword

[英]set bootPassword for derby database at runtime using hibernate

我使用Hibernate和Derby。

我有一个hibernate.cfg.xml,我所做的所有与db waas一起使用来获取Session的工作:

  return new AnnotationConfiguration().configure( "files/hibernate.cfg.xml"   ).buildSessionFactory().getCurrentSession();

我的hibernate.cfg.xml包含

   <property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
   <property name="connection.url">jdbc:derby:crmdb;create=true</property>

以及实体类的其他一些属性和映射。

现在我想在运行时为derby db和bootPassword设置dataEncryption。

我改变了hibernate.cfg.xml:

    <property name="connection.url">jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword=myPass</property>

一切都还好。

现在,我想在运行时设置bootPassword,例如从环境变量中读取。 那就是问题所在! 当我从hibernate.cfg.xml删除“ connection.url”并尝试在我的代码中设置它时,发生此错误:

 java.lang.UnsupportedOperationException: The application must supply JDBC connections

如果我仅删除bootPassword,它就无法连接到数据库。

任何想法 ?

解决了!

我应该设置“ hibernate.connection.url”而不是“ connection.url”!

您需要在构建sessionFactory之前修改配置:

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory(){
    if(sessionFactory==null){
        Configuration configuration = new AnnotationConfiguration().configure( "files/hibernate.cfg.xml"   );
        configuration.setProperty("hibernate.connection.url", "jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword="+password);
        configuration.configure();
        sessionFactory = configuration.buildSessionFactory();
    }
    return sessionFactory;
}

其他说明:您必须避免每次都重新创建一个新的SessionFactory(这会花费很多时间,消耗大量资源并且没有用)。 即您必须为每个bootPassword创建一个sessionFactory(因为它是唯一的动态部分),并且如果只有一个bootPassword-即一个DB-那么sessionFactory可以/必须是单例。

REF

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM