Hosting an H2 database and accepting connections - java

I've looked all over for this for quite a while so I'm just going to ask it here;
How do I set up an H2 db in server mode so I can connect to it via the internet from a different machine? How do I start the engine in server mode and leave it running on a machine to accept connections? I can forward the ports and everything fine, it's just getting the engine in "receiving" mode that I'm dumb about.
I'm sorry, I've really looked everywhere. I want to be able to connect to the db and add data from a mobile app. All of the app and transmission data is working great, I just need to be able to run a server with the db on it and receive the data. Any tutorial or documentation that is clearer than the stock H2 server mode documentation would be really appreciated.
Thanks!

For security reasons, by default the H2 servers (including the TCP server) are protected against remote access. The error message you get on the client should be clear this. You have to explicitly enable remote access using -tcpAllowOthers (for the TCP server):
java -cp h2.jar org.h2.tools.Server -tcp -tcpAllowOthers
This will only start the TCP server - see the documentation for details.

$ java -cp h2.jar -Dh2.binAddress=0.0.0.0 org.h2.tools.Server
See http://h2database.com/html/advanced.html#server_bind_address

Related

How can we allow TCP/IP connections for SQL Server on Docker?

I'm learning Java EE, and I'm trying to open a connection to my SQL Server database running on a Docker image. I obviously get the
Cannot create PoolableConnectionFactory (The TCP/IP connection to
the host localhost/DB_DEMO, port 1433 has failed.
With
docker ps -a
I get 0.0.0.0:1433->1433/tcp under "Ports" for my SQL Server image.
I know that we can use SQL Server Configuration Manager to allow TCP/IP connections, but I can't seem to find how to install it without installing the full SQL Server suite.
So, is there a way to enable TCP/IP on my SQL Server running in a Docker container? Or something I don't get in the whole "TCP/IP" thing?
EDIT [SOLVED]:
I get this error with this url (Microsoft docs said 1433 is used by default if port isn't specified):
url="jdbc:sqlserver://localhost/DB_DEMO"
It worked using the {property=key} format:
url="jdbc:sqlserver://localhost;databaseName=DB_DEMO"
I've been doing too much tweeking at once while going back and forth these two formats, and my bad, I thought the problem wasn't there (I took the server/DBName format from a a documentation though). Lesson learned!
Like Mark Rotteveel replied, the TCP/IP is certainly enabled by default on the Docker image anyway.

H2 Database Auto Server mode : Accessing through web console remotely

I am fairly new to H2 Database. As a part of a PoC, I am using H2 database(version : 1.4.187) for mocking the MS SQL Server DB. I have one application, say app1 which generates the data and save into H2. Another application, app2, needs to read from the H2 database and process the data it reads. I am trying to use Auto Server mode so that even if one of the application is down, other one is able to read/write to/from the database.
After reading multiple examples, i found how to build the h2 url and shown as below:
jdbc:h2:~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
Enabled the tcp and remote access as Below:
org.h2.tools.Server.createTcpServer("-tcpAllowOthers","-webAllowOthers").start()
With this, I am able to write to the database. Now, I want to read the data using the h2-web-console application. I am able to do that from my local machine. However, I am not able to understand how I can connect to this database remotely from another machine.
My plant is to run these two apps in an ubuntu machine and I can monitor the data using the web console from my machine. Is it not possible with this approach?
How can I solve this ?
Or do I need to use server mode and explicitly start the h2 server? Any help would be appreciated.
By default, remote connections are disabled for H2 database for protection. To enable remote access to the TCP server, you need to start the TCP server using the option -tcpAllowOthers or the other flags -webAllowOthers, -pgAllowOthers
.
To start both the Web Console server (the H2 Console tool) and the TCP server with remote connections enabled, you will have to use something like below
java -jar /path/to/h2.jar -web -webAllowOthers -tcp -tcpAllowOthers -browser
More information can be found in the docs here and console settings can be configured from here
Not entirely sure but looking at the documentation and other questions answered previously regarding the same topic the url should be something like this:
jdbc:h2:tcp://<host>:<port>/~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
It seems that the host may not be localhost and the database may not be in memory
Is there a need for the H2 web console?
You can use a different SQL tool using the TCP server you have already started. I use SQuirreL SQL Client (http://squirrel-sql.sourceforge.net/) to connect to different databases.
If you need a web interface you could use Adminer (https://www.adminer.org/) which can connect to different database vendors, including MS SQL, which happens to be mode you're running H2. There is an Adminer Debian package that should work for Ubuntu.

How to debug a jar remotely from netbeans

I'm trying to figure out how to debug my jar that is running remotely. Here is my scenario:
My .jar will be running from a VPS. This jar basically runs a server
for a game, so it also connects to a mysql db. I start the server with 3 .bat files that looks something like this:
set CLASSPATH=.;dist\aries.jar;dist\mina-core.jar;dist\slf4j-api.jar;dist\slf4j-jdk14.jar;dist\mysql-connector-java-bin.jar
java -Xmx500m -Dwzpath=wz\ -Djavax.net.ssl.keyStore=filename.keystore -Djavax.net.ssl.keyStorePassword=passwd -Djavax.net.ssl.trustStore=filename.keystore -Djavax.net.ssl.trustStorePassword=passwd net.world.WorldServer
pause
What I want to do is start the server on the vps like normal, but debugging the server on my local machine via Netbeans IDE. I don't know if this is possible because people will be connecting to the server (although, I will be debugging a test server which will only have me online).
Note: I have done a lot of searching before coming here and a lot of what I found had to do with using xdebug & php which has doesn't have much to do with my situation (I don't think)
-Thanks
There's a NetBeans FAQ page about this.
In brief:
Add the remote debugging options to your Java command. For example:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8888,suspend=n
Then, use the attach debugger option in NetBeans and select your server and the port you used above (8888). It's pretty much that easy.
You may want to think about network and firewall considerations, as you may have noticed there are no passwords involved, so anyone who can connect to the port can debug your app. This could be a big security risk. Your VPS provider probably has some tools to help with setting up a secure, private connection.

Java server on OpenShift

At the moment i have an android client app which connects to my java server through socket - serversocket. It sends and receive strings. The java server is connected to a mysql database (actually mariadb) using the jdbc driver.
I succeed to create a jbossas application and upload the code of the java server to openshift, but i didn't find any detailed tutorial on how do i connect to this new uploaded server from my socket client (This one (RMI or socket connection to Java Program on OpenShift) gives some tips but i'm still stucked).
More on this, how do i know that my server runs just fine on openshift and how do i control de calls to the database after i connect it (found this: $ rhc app create MyApp jbossas-7
$ rhc cartridge add mysql-5.5 -a MyApp), using org.mariadb.jdbc.Driver and java.sql is still working ?
Any small guide or tip is highly appreciated. I'm new to these things so please don't be too heavy on comments.
You can only make connections to your OpenShift server on http/https or ws/wss ports. If you want to connect to your java application and pull data from it from an android device, I would suggest using a RESTful api or a servlet, etc.
I had similar problem: My app server originally was running as a ServerSocket listener, and any clients/devices connect to it directly via Socket binding.
To deploy it into OpenShift, my previous initial solution was to change its host:port configuration by following the suggestion as described in this link [Socket connection to Java Program on OpenShift]. It worked nice as far as my app server was successfully up and running. But it did not work well with the port forwarding approach in order to accept remote requests.
So for the final solution, I modified the app server by wrapping my original code with a RESTful webservice around it, and deploy it as a web service.

App Java and hosting mysql

I have a Java application and I have to connect to a MySQL DB host in aruba.it. If I make a connection, aruba.it refuses that. How to solve this?
To start, I assume that you're trying to run this Java application locally, or at least at a different machine than where the MySQL DB runs and that you got a SQLException: Connection Refused.
To fix the particular problem, all routers and firewalls in the complete network pipe between the client (where the Java application runs) and the server (where the MySQL DB runs) needs to be configured to allow/forward the port number which the DB uses. This is by default 3306. If this port is blocked, you cannot reach the DB from outside.
Another solution is just to upload the Java application in flavor of a webapplication and run it by HTTP. You'd normally use JSP/Servlet for this.
Apart from network, routers, firewall issues the reason can be that by default remote access to MySQL database server is disabled for security reasons. Mostly DB is hosted on the same server or on the trusted server. If you run java application from your desktop, you need to configure MySQL so it will accept this connections. See this manual for details how to do it.

Categories