TCP <localhost> Connection Issue.

556 views
Skip to first unread message

AGupta

unread,
May 25, 2010, 12:05:43 PM5/25/10
to H2 Database
Hi,

I am trying a simple example using the H2 in memory database.

Experiment: Want to create an in memory Database and see the contents
of this in-mem database using a browser.

Tried Solution: Used Spring to create the DBCP Pool for H2 db. Here is
the spring config.

SPRING CONFIG:
<bean id="h2Server" class="org.h2.tools.Server" scope="singleton"
factory-method="createTcpServer" init-method="start"
destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,9092" />
</bean>

<bean id="h2TCPDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" depends-on="h2Server">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:tcp://localhost:9092/
tcpTest;MODE=Oracle;DB_CLOSE_DELAY=-1;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>


JAVA CODE:
public static void main(String[] args) throws SQLException {
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("spring/spring-h2-database.xml");

DataSource h2ds = (DataSource) ctx.getBean("h2TCPDataSource");
Connection conn = h2ds.getConnection();
conn.createStatement().execute("create table MYTABLE (id INTEGER,
name varchar(20), salary number)");
conn.createStatement().execute("insert into MYTABLE values(1,'AMIT',
20000000.0)");

System.out.println("Row Inserted. Check on Console");
}

When opening a JS enabled Browser with URL (http://localhost:9092/):

OUTPUT:
(AbstractStringBuilder.java:45) at java.lang.StringBuilder.
(StringBuilder.java:80) at
org.h2.value.Transfer.readString(Transfer.java:240) at
org.h2.server.TcpServerThread.run(TcpServerThread.java:78) ... 1 more

I know I am doing something wrong but what?

Thanks,
Amit-G.

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Kerry Sainsbury

unread,
May 25, 2010, 4:44:31 PM5/25/10
to h2-da...@googlegroups.com
I'm not sure what your exact problem is, http://www.h2database.com/html/features.html#in_memory_databases says the correct syntax for an in-memory database that is accessible via tcp-ip is something like:

jdbc:h2:tcp://localhost:9092/mem:tcpTest;...

not 

jdbc:h2:mem:tcp://localhost:9092/tcpTest;...

.. which looks like it could be significant.

Cheers
Kerry

Thomas Mueller

unread,
May 27, 2010, 1:20:38 PM5/27/10
to h2-da...@googlegroups.com
Hi,

There are multiple problem. First, you can't connect with a browser to
the TCP server. You need to start a "web" server (H2 Console). See
http://h2database.com/html/tutorial.html#tutorial_starting_h2_console
and http://h2database.com/html/tutorial.html#using_server - the
documentation is not very clear about that, I will try to improve it.

> Tried Solution: Used Spring to create the DBCP Pool for H2 db. Here is
> the spring config.

I suggest to *not* try Spring or a connection pool until you have
understood using the database with the JDBC API, and using the H2 API.
Once you have done that, it's relatively simple to "add" Spring and to
"add" a connection pool. I'm not saying Spring or connection pools are
bad, but it's added complexity (more possibilities for bugs) you
should avoid until the basics work.

> jdbc:h2:mem:tcp://localhost:9092/tcpTest;MODE=Oracle;DB_CLOSE_DELAY=-1;AUTO_SERVER=TRUE

There are two bugs here. First, AUTO_SERVER with memory database are
not supported. Second, you have the "mem:" at the wrong place. You
probably want:

jdbc:h2:tcp://localhost:9092/mem:tcpTest;MODE=Oracle;DB_CLOSE_DELAY=-1

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages