Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate database password at runtime

Is there a way to ask for the database password at runtime instead of putting it (encrypted or not) in the hibernate.cfg.xml file?

like image 798
Erik Stens Avatar asked Dec 16 '10 13:12

Erik Stens


People also ask

Which property used to specify the location of the database server in hibernate?

Hibernate Datasource Properties It represents datasource JNDI name which is used by Hibernate for database properties. It is optional. It represents the URL of the JNDI provider.

Which of the following is used to configure the driver class of a database?

The JDBC driver class. The JDBC URL to the database instance.


3 Answers

Just about every configuration option in Hibernate has a corresponding method on the object being configured. In reality, the configuration is really just a way to bind XML to the objects being set up. See this article for more information: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

That said, the onus is on you to collect the password at startup. That can be the most difficult part of the problem. Once you've collected the password, send it to the appropriate property.

like image 96
Berin Loritsch Avatar answered Sep 30 '22 12:09

Berin Loritsch


Usually the best way to do it, if you're using a Java EE app server, is to use a JNDI look up to get the database connection instead of using a driver manager. That way the person who sets up the JNDI connection pool is the only one that has to know the password, and it's generally encrypted in the admin console so it's safe.

like image 26
duffymo Avatar answered Sep 30 '22 11:09

duffymo


I think if you are using programmatic instantiation of the Hibernate configuration, you can initialize it from the configuration file that does not contain a password, set the additional property for the database connection on the configuration object you're instantiating, then call buildConfguration().

like image 34
Dave G Avatar answered Sep 30 '22 11:09

Dave G