Reputation: 21
I've just created a database on OpenShift and I'm trying to use Hibernate. My configuration file is as follow:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/mytomcatapp</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">pwd</property>
But it doesn't work. When I print my env variables, the result is:
OPENSHIFT_MYSQL_DB_HOST:127.11.10.2 OPENSHIFT_MYSQL_DB_PORT: 3306 OPENSHIFT_MYSQL_DB_USERNAME: username OPENSHIFT_MYSQL_DB_PASSWORD: passwd OPENSHIFT_MYSQL_DB_URL: mysql://username:[email protected]:3306/
is there anything wrong?
Upvotes: 2
Views: 842
Reputation: 109
This is wrong: the application uses the configuration file as text, not as code.
To solve this problem you can use dynamic Hibernate configuration with Spring.
Here is an example how to do it: Script Security + Hibernate Anotation Example.
In case of using openshift you need to some changes in the file AppConfig.java:
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
String name = "mytomcatapp";
String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
ds.setUrl(url);
ds.setUsername("username");
ds.setPassword("pwd");
return ds;
}
Upvotes: 2