I'm using the H2 Database Engine for java to have access to a database in my java programs. I developed many java programs which use the same database. The problem is that whenever I start such a program while another is already running it can't access the database because it is opened by the other program.
Is there a way to let both programs have a connection to the database? Whenever one program has to query the database the database should execute the query. In case it is executing the query of the other program the query should be executed directly after the query of the other program. Since my queries don't take long time the user wouldn't recognize that his program has to wait for a moment and everything would be fine.
H2 server mode is what you want.
You need to have at least started the server this way for example:
org.h2.tools.Server.createTcpServer().start();
Then replace all the jdbc url with jdbc:h2://yourhost/yourdb, keeping in mind yourdb.h2.db will be located where the server was started. I strongly advise not to use absolute path in your jdbc url, as it will give away the database path in case of hacking.
Last but not least: using the server mode for all has a performance penalty. You might want to use mixed mode so that the 1st client will have almost embedded performances.
To do this, just replace for this client the url with jdbc:h2:yourdb;AUTO_SERVER=TRUE. You could decide to use the same url for all clients as well: the 1st one to connect will be using embedded mode, the other clients will be using tcp performance.
Note that if you're using H2 > 1.4.*, you need to give absolute or relative path like this: jdbc:h2:./yourdb;AUTO_SERVER=TRUE. Note the ./
Related
I have created a java application that is inserting data to a mysql database. Under some conditions, i need to post some of these data via email, using a java application that I will write as well.
My problem is that i am not sure how i should implement this.
From what I understand, I could use a UDF inside MySql to execute a java application, for which there are many against opinions into using it. Let alone that both the database and the mail client application will reside in a VM that i dont have admin access, and dont want to install anything that neither me nor the admin knows.
My other alternative, that I can think of, is to set up the mail client (or some other application), to run every minute just to check for newly inserted data. Is this a better aproach? Isn't it going to use resources for doing almost nothing. At the moment the VM might not be heavily loaded, but i have no idea how many applications there might end up running on the same machine.
Is there any other alternative i should consider using?
You also need to consider the speed of internet, database server load, system resources. If you have enough memory and less load to insert data in databases or database load is not so much then you can approach this by cron setup. For linux call a script for every 5 minutes. The script perform the following-
1. Fetch unread Emails as files
3. Perfrom shell script to read needed data.
3. write data to mysql
4. Delete the email
If you have heavy loaded system then wise you need to do this once or twice in an hour or may vary.
I have a program that I've created that is meant to poll an html internal page with different IPs that update and then will run a telnet session to those IPs to see if the device still has a connection... I'm attempting to challenge myself in creating something further with a dynamic webpage instead of my program spitting out console output...
My Issue:
I dont know what technologies / libraries Java has to execute such things
I want:
A Local Server, to upload a page LOCALLY only (no security is needed as this will be strictly intranet)
My program to implement: A database of sorts to save "logs" essentially that a certain IP / device has had successful connections
in the past....maybe stored to an external file is fine i presume (my
program currently has to re-poll everytime i run it.. i want some kind
of "remembering"..
Is it possible this can all be done in one file? so if i want my computer to run this as soon as it starts up... it will run... grab
its current state of the database of IPs... poll them (periodically)
and then persist and save and update the HTML page dynamically....
I hope i'm being as descriptive as possible... Its a bit of an abstract.. I really just want some introduction to different libraries ... a friend recommended stuff like MongoDB or something but I want to stay strictly to Java programming
I'm trying to write a code where i need to keep some data in database tables.
I have learned how to use SQL. My problem is that I want this program to run on different computers while having access to the same database.
Any suggestions ?
Thanks
You'll have to run an SQL server on a computer (ie: with mySQL, SQL Server, etc.). You will run queries against the tables that are stored on this database. There are already a lot of resources online with making a database connection to a server.
Just keep database system running in a machine, and instead of pointing to localhost in your JDBC URL, point to the machine's IP Address.
I would personally recommend setting up a script on a host machine and letting it communicate with the database, and have your program communicate with the script. This way, you won't have to keep your database login credentials in your Java program, which could be dangerous.
I'm looking for a way to test if my application can survive if the database it connects to goes down and back up again. In which case the expected behaviour of the application would be to first throw an exception and then reconnect successfully once the database is up.
I would like to do so in an automated integration test (so manually powering off/on the database and see how the app behaves is out of question).
Connecting to an in-memory database is an option but this will devalue the test somewhat since the prod code and test code wont run against exactly the same db and drivers, so not ideal.
Another option would be for the test to trigger a process which would cutoff the connection between the database and the app for a few seconds (not sure how to do that).
Any other ideas ?
Technical stack is Java with Spring (jdbc templates).
I think you can try to supply with incorrect db log in information to simulate the connection loss
If you are looking at killing the connection, depending on your setup you could create a batch script to disable your network adapter, and another switch it back on again once you are ready
This might help
How can I write a batch file to toggle my network adapters?
and you can use this to call the script
How do I run a batch file from my Java Application?
I have a Java application that sends user score to the mysql table. When the user is done, Java app accesses the .php file on server and the .php server performs a query on the database (inserting score).
I am concerned about the (in)security of this method. I mean, if someone finds out the direct url to the .php on a server, they can produce a lot of mess in the dabase. Can you advise how I could prevent the .php from executing the query other than accessed by the Java app?
edit: The problem is that Java application is NOT run on the server, it's run on the user computer using Java Web Launcher platform. So it's not an applet...
The problem is conceptual. You should never be sure that users can't find out the real address (security by obscurity). You could use SSL, still this is no means against a good guess.
Since the Java program is run on the client side, a .htaccess restricting access to a certain IP is also not an option.
My suggestion is to create a separate user in mysql, grant this user access only to necessary tables and perform the database queries on behalf of this user directly in Java. This way all data is encrypted (see http://dev.mysql.com/doc/refman/5.5/en/ssl-connections.html) and no URL/access point is exposed. Of course it means your MySQL server must be reachable from outside which poses a risk, too. You should have a good root password!