H2 Multiple connection - spring

I have two applications : app1 and app2 , and i want that this two application use the same H2 file as a database .
I test this configuration in the two sides but it didn't work :
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/dBTrunk;MODE=Oracle;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
I am open to all kind of solution my only goal is that app1 and app2 can work in parralel mode with the same H2 database .
Regards

This should work. The first app should open to the database file in embedded mode, but then start a database server. The second app will then try to connect to the first app's database server.
If it is not possible for the two apps to talk to each other using TCP/IP, then it wont work. Is there a firewall between them? Are there certain ports blocked?

Related

How to make JNDI look up optional in Spring

Is there anyway where we can make the JNDI lookup optional in spring appllicationcontext xml configuration.
I want to deploy the same application which has JNDI setting(Data base connection) on two different environments. In one environment we need DB connection and another environment we don't need the DB connection. Could you please suggest if there is a away we can achieve this without modifying the applicationcontext.xml(I mean without commenting out the JNDI configuration and other related bean injection for DB connection).
Use profiles, something like:
<beans profile="prod">
<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/DatabaseName"expected-type="javax.sql.DataSource" />
</beans>
<beans profile="dev,default">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="connectionCachingEnabled" value="true"/>
</bean>type="javax.sql.DataSource" />
</beans>
Then when you start you app say which profile with system argument:
-Dspring.profiles.actibe=prod
The default profile will be dev.

How to use different configuration for deployment?

I am currently dealing with the deployment of my web-server which is why I am facing a presumably very common problem: Configuration files
As a very straight forward example let's take a database. I have a local database.xml which contains the data source which I use on my local machine:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/mz_db" />
<property name="username" value="postgres" />
<property name="password" value="pg" />
</bean>
but of course, I am connecting to a different database once I deploy the server. So what are my options here? I would like to have this as centralized as possible to prevent wrong files being deployed here. I am sure that maven can be used for this somehow.
I was thinking about doing something like creating two config/ directories
WEB-INF/config/
WEB-INF/config-dev/
and use config-dev/ per default without the exception of the deploy phase or something like this. I am not sure if this can be done or if this is the best way of doing it.
Any suggestions?

Postgresql database location for maven build tests

I have big maven project. So I run mvn clean install. But I get errors on tests. The sql connection exception. I have sql file with database for Postgresql. Should I have create database (with user and password) on localhost? So how do junit tests run? Do they need server with database on localhost?
This xml for connection database:
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost/lion" />
<property name="user" value="osm" />
<property name="password" value="101918" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="5" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="maxStatements" value="0" />
<property name="maxIdleTime" value="100" />
</bean>
Yes, apparently you have a project with Spring integration tests connecting to real PostgreSQL database. Most likely other developers have such a database installed so it works for them. Not perfect but sometimes necessary.
That being said yes, you need to isntall PostgreSQL, create lion database and all needed tables. Moreover database user name and password are apparently hard-coded in your configuration (should they be posted here?), so you must either create the same user in your PostgreSQL server or (better) externalize jdbcUrl, username and password.
Finally consider incorporating flyway, brilliant little library that will create your database tables if you start an application on empty schema.

Tomcat Pool Empty

We are using Tomcat 6 with tomcat-jdbc.jar and tomcat pooling (we are currently using the latest version 7.0.28, but have tried with previous versions as well).
This is using PostgreSQL on the back-end.
About once every 2 - 5 days (it's rather "random" in happening), our server uses up all of the available db connections, in about a 10 minute period, and we see these in the logs (we've turned on FINE logging):
2012-10-01 18:40:29,998 ERROR [TP-Processor29] JDBCExceptionReporter.logExceptions(72) | [TP-Processor29] Timeout: Pool empty. Unable to fetch a connection in 0 seconds, none available[size:150; busy:41; idle:0; lastwait:0].
2012-10-01 18:40:30,000 ERROR [TP-Processor29] JDBCExceptionReporter.logExceptions(72) | [TP-Processor29] Timeout: Pool empty. Unable to fetch a connection in 0 seconds, none available[size:150; busy:41; idle:0; lastwait:0].
I'm trying to track down the root cause problem of this.
When looking at the db and this occurs, all of the connections are shown as "IDLE".
Our current configuration looks something like this:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://db/dbname?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
<property name="initialSize" value="8"/>
<property name="maxActive" value="150"/>
<property name="maxIdle" value="10"/>
<property name="maxWait" value="30"/>
<property name="defaultAutoCommit" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="validationInterval" value="60000"/>
</bean>
And, our logging in logging.properties is:
org.apache.tomcat.jdbc.pool.level=FINE
Most of the time, we are using about 15 - 20 database connections, however, as I mentioned, the problem is that every few days, it rapidly uses up all of the connections, and requires a restart.
We're trying to track this down to being a Tomcat pool issue, a Hibernate issue, a Spring issue, or an app issue.
To start with, any ideas why:
A) Why does it show: [size:150; busy:41; idle:0; lastwait:0] ... where are the other 150-41 connections?
B) When logging, it shows when the connection opens, but not when it closes. How can we turn that on?

Start the H2 database in server mode via Spring

I'm trying to start the H2 database in server mode (I want it to run in a different process) via Spring.
Currently I'm using java Runnable.exec to start the h2 database (using the command: "java -cp h2.jar org.h2.tools.Server")
I know that there is a way to do it via Spring. I tried to add the following to the spring configuration, but it didn't work (it didn't start the H2 database):
<bean id="org.h2.tools.Server" class="org.h2.tools.Server"
factory-method="createTcpServer" init-method="start" destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
</bean>
<bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
factory-method="createWebServer" init-method="start">
<constructor-arg value="-web,-webAllowOthers,true,-webPort,8082" />
</bean>
I would appreciate any help/ideas
Do you happen to have:
<beans default-lazy-init="true" ...
in your Spring configuration files?
Recently I had to do the same configuration to make unit test and check data, this works for me (Spring 3.1.4). Then you just have to connect with jdbc:h2:tcp://localhost:8043/mem:test and make sure to put a while(true){} at the end of your test.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<!--property name="url" value="jdbc:h2:mem:;TRACE_LEVEL_FIlE=4"/-->
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop">
<constructor-arg>
<array>
<value>-tcp</value>
<value>-tcpAllowOthers</value>
<value>-tcpPort</value>
<value>8043</value>
</array>
</constructor-arg>
</bean>
Are you sure the createTcpServer method in the Server class is really called? Have you tried setting up a breakpoint there?
H2 tutorial claims this how you can create and start the server programmatically:
import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();
Your Spring definition seems to mimick the same initialization. But you can always try doing it manually - maybe it's some fault in Spring configuration.
EDIT:
I've tried your configuration and it works for me. What makes you think the server is not started? It doesn't print out anything on stdout, however the process listens at the 8043 port. So it seems pretty OK.

Resources