Database connection - best practice - h2

I'd like to know what is the best practice to connect to H2 database when using embedded mode, e.g. in standalone Java desktop application.
Currently, I have the database file (.h2) in the same directory as main application jar file and using standard jdbc:h2:file: prefix to locate the file on the disc. Of course, this can be portable only if you can get the absolute path of the file dynamically, i.e. as relative path to the main Jar.
I am using this:
class.getProtectionDomain().getCodeSource().getLocation().getPath()
and its working. But I am not sure if its completely portable. I didn't try it on all the OS that Java supports.
Is there any other (maybe better) way to do this?
Regards,
Lubos

Related

How to intercept the database file used by a Delphi application?

I have entered data into a Delphi application for analysis, but the application exports the data into a messed up comma separated file (CSV). It does not export a database file.
I looked at the application binary and found out it is using SQLite3, given the linking symbols in its portable executable (PE).
Is there a way to find if the application is saving a file temporarily somewhere in my system?
If so, how to find it on Windows? Can I track files created by a process?
I would like to try anything before running the application and instrumenting the sqlite3_* function calls, since I don't know how to do that with WinDbg yet.

Is there a way to append/remove a resource to a binary at execution time?

Is it possible to append/remove a ressource file to a binary at execution time?
I have an application written with go, which saves/searches data from a database file, and i would like this database file to be embedded to the binary, and updated by the application itself.
This way the application would be self contained with its database.
Modifying the executable, this is generally a very bad idea.
Several issues pop right into my head, such as:
Does the current user have sufficient permissions?
Is the file locked during execution?
What about multiple running instances of the application?
Even if you manage to do just that, think of what anti-virus and firewall applications will say to it: most when they detect the change will flag the executable and/or contain it, or deny running it, or some may even delete it. Rightfully, as this is what many viruses do: modify existing executables.
Also virus scanner databases maintain reports where files (their contents) are identified based on the hash of their content. Modifying the executable will naturally change the file content hash, thus render the file unknown / suspicious to these databases.
As mentioned, just write / cache data in separate file(s), preferably in user's home folder or in the application folder (next to the executable, optionally in sub-folders). Or make the cache file / folder a changeable option (command line flags).
Technically, this is possible, but this is a bad idea. Your application could be run by users not having write permissions to your binary.
If you're talking about a portable app, your best option might be using a file in the same directory the binary is located, otherwise - use the user's home directory according to the conventions of the OS you're running on. You can use the os/user package to find the home directory.

Making an SQLite ConnectionString portable

As a personal project I want to test the ability of using SQLite with MVC 3 and so far so good managed to get everything setup.
The thing I am now trying to achieve is to make the solution fully portable during development, but I am unable to achieve this as the connection string is complaining that the database is missing if I try on different machines where the solution it located in different places.
Is there any way that I can alter the connection string so it is uses a set directory each and everytime?
I have the database housed in a datalayer solution in the same folder houses my datamodel if that is any help
I see two ways:
Specify the location of the SQLite database as relative to the executable
Make the path specifiable as input (say a command line argument or .config file)

Create file in netbeans platform

I've made a plugin for NetBeans which parse a Java file and generate 2 files. I'm writting it to the disk with standard operations (i.e., normal java.io.File). It works, but it takes a while (usually 10 seconds or +) to show on the project (folder) tree.
I was wondering if there is a more efficient way to create this file using the netbeans platform api.
The FileSystem API is the place that you should probably start. You may want to focus on the FileObject and FileUtil classes.

log4j writing to a windows share

I have a java application running on windows machines.
Long story short, we have a convention for where we place log files per machine:
\\%COMPUTERNAME%\Logs\<AppNameHere>
So I configured my Java app to startup with -Dmachine.name="%COMPUTERNAME%", and then in my log4j.properties file I specify
log4j.appender.R.File = \\${machine.name}\Logs\MyVerySpecialApplicationName\log.log
But I'm not seeing that directory / file show up when I run my application (the first thing the app does is log a startup message).
So my guess is that log4j / java can't process that windows specific UNC path.
Anyone else run into this issue and figure out a way around it?
I looked at Log4j's source code. It appears to use java.io.File to hold a reference to the filename you specify.
Also, the Javadocs for java.io.File state that UNC paths are supported for the constructor of File (which Log4J uses).
So, on the surface, there's no reason why your configuration won't work; but — and that's the important point to note — Java has a long history of problems with file I/O over SMB (which is pretty much what you're trying to do).
My advice:
Start your application by specifying -Dlog4j.debug=true. The system property will make Log4J spit lots of debug information to help you track the problem.
Attempt using the same configuration, except that, instead of referring to the file with a UNC prefix, simply map the drives (I understand that you're running on Windows). If things work with mapped drives, it means that something in using the UNC prefix is the source of the problem (although I'd doubt it).
You need to put two backlash for each one like in this configuration below
log4j.appender.Log_Arquivo.File=\\\\172.31.88.168\\server10\\soma_10\\logs\\soma_10.log
I hope this help you.

Resources