title: java.net.ConnectException: Connection refused: connect: localhost
body: <p>I am trying to connect to h2 database from a hibernate project.</p>
  
  <p>It worked fine with Oracle but I need to migrate to H2.
  The only things that I have changed are the driver and the hibernate.cfg.xml</p>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;com.h2database&lt;/groupId&gt;
      &lt;artifactId&gt;h2&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>hibernate.cfg.xml file</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
      &lt;property name="connection.url"&gt;jdbc:h2:tcp://localhost/~/test;IFEXISTS=TRUE&lt;/property&gt;
      &lt;property name="connection.username"&gt;sa&lt;/property&gt;
      &lt;property name="connection.password"&gt;&lt;/property&gt;
      &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
      &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
      &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;mapping class="com.wkfsfrc.drools.dummy.runtimeload.Employee"/&gt;
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>the exception that I get is:</p>
  
  <pre><code>    May 19, 2015 11:32:49 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
  Initial SessionFactory creation failed.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
  
  java.lang.ExceptionInInitializerError
      at com.wkfsfrc.drools.dummy.runtimeload.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
      at com.wkfsfrc.drools.dummy.runtimeload.HibernateUtil.&lt;clinit&gt;(HibernateUtil.java:9)
      at com.wkfsfrc.drools.dummy.runtimedataload.RuntimeDataLoadTest.&lt;init&gt;(RuntimeDataLoadTest.java:25)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
      at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
      at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
      at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
      at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
      at com.wkfsfrc.drools.dummy.runtimeload.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
      ... 27 more
  Caused by: org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-178]
      at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
      at org.h2.message.DbException.get(DbException.java:167)
      at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:438)
      at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:315)
      at org.h2.jdbc.JdbcConnection.&lt;init&gt;(JdbcConnection.java:107)
      at org.h2.jdbc.JdbcConnection.&lt;init&gt;(JdbcConnection.java:91)
      at org.h2.Driver.connect(Driver.java:74)
      at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
      ... 41 more
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:579)
      at org.h2.util.NetUtils.createSocket(NetUtils.java:123)
      at org.h2.util.NetUtils.createSocket(NetUtils.java:103)
      at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:100)
      at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:434)
      ... 46 more
  </code></pre>
  
  <p>What else do I need to do, to successfully connect to H2.</p>
  
  <p>Do I need to start H2 before connecting to it or Hibernate does this? DONO</p>
  
  <p>Any advice is appreciated!</p>