title: Recommended Hibernate configuration for using a local and a Heroku postgres database
body: <p>I am developing a <code>Jersey</code> application which is connected to a <code>PostgreSQL</code> database. I am looking for a solution how to configure <code>Hibernate</code> in a way that it always connects correctly to either the local or the <code>Heroku</code> based database (depending on if I deploy my application locally or if I push it to <code>Heroku</code>). </p>
  
  <p>Using the <code>Heroku</code> guides, I tried something like this:</p>
  
  <p>HibernateUtil.java:</p>
  
  <pre><code>private static SessionFactory buildSessionFactory() {
      try {
          // Create the SessionFactory from hibernate.cfg.xml
          Configuration configuration = new Configuration();
          configuration.configure("hibernate.cfg.xml");
  
          configuration.setProperty("hibernate.connection.url",
                  System.getenv("DATABASE_URL"));
  
          URI dbUri = new URI(System.getenv("DATABASE_URL"));
  
          String username = dbUri.getUserInfo().split(":")[0];
          String password = dbUri.getUserInfo().split(":")[1];
          String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
                  + dbUri.getPort() + dbUri.getPath();
          configuration
                  .setProperty("hibernate.connection.username", username);
          configuration
                  .setProperty("hibernate.connection.password", password);
          configuration.setProperty("hibernate.connection.url", dbUrl);
          System.out.println("Hibernate Annotation Configuration loaded");
  
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                  .applySettings(configuration.getProperties()).build();
          System.out.println("Hibernate Annotation serviceRegistry created");
  
          SessionFactory sessionFactory = configuration
                  .buildSessionFactory(serviceRegistry);
  
          return sessionFactory;
      } catch (Throwable ex) {
          // Make sure you log the exception, as it might be swallowed
          System.err.println("Initial SessionFactory creation failed." + ex);
          throw new ExceptionInInitializerError(ex);
      }
  }
  </code></pre>
  
  <p>My hibernate.cfg.xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 4.0//EN"
          "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Database connection properties - Driver, URL, user, password --&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:postgresql://localhost/MYAPP&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;user&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;password&lt;/property&gt;
  
          &lt;!-- org.hibernate.HibernateException: No CurrentSessionContext configured! --&gt;
          &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Mapping with model class containing annotations --&gt;
          &lt;mapping class="com.example.object" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>The idea is that HibernateUtil overrides the hibernate.cfg.xml properties (url, user, password). Deploying locally works but deploying to Heroku fails:</p>
  
  <p>remote:        [ERROR] Failed to execute goal de.juplo:hibernate4-maven-plugin:
  .1.0:export (default) on project myapp: Execution default of goal de.juplo:hib
  rnate4-maven-plugin:1.1.0:export failed: Error calling Driver#connect: Connecti
  n to localhost:5432 refused. Check that the hostname and port are correct and t
  at the postmaster is accepting TCP/IP connections. Connection refused -> [Help
  ]</p>