In SimpleORM (a JAVA ORM library) examples, I see a code like:
java.sql.DriverManager.getConnection("jdbc:hsqldb:hsqlTempFiles;shutdown=true;", "sa", "")
I am not able to understand the meaning of hsqlTempFiles in above. My basic understanding of hsqldb consists of file/mem/res/hsql based access modes. In SimpleORM code archive, I do see a file named hsqlTempFiles.properties and hsqlTempFiles.script.
Is this an older way to refer to file based hsqldb databases?
The part after hsqldb: is the name of the database to connect to. Specifying the name of the file without file: may just be legacy or backward-compatible logic.
Related
So I'm using derby for a patient management learning project. I'm using the following code to set the system property for derby's database write location..
Path path = Paths.get(System.getProperty("user.home"), "patient_management_databases");
System.getProperties().setProperty("derby.system.home", path.toString());
log.info(String.format("Set derby home to '%s'.", path.toString()));
My question is whether this is good practice on an application that is meant to be distributed and used in production? Changing the derby home location could cause problems in other applications using derby (I'm assuming all derby applications will access this property). Is there a way to set the path for this application only? It was saving to my project folder before which isn't ideal because of IDE indexing and having to ignore them when using VCS.
I was able to get the behaviour I wanted with the following code:
private final Path databaseDirectory = Paths.get(System.getProperty("user.home"), "patient_management_databases");
String.format("jdbc:derby:%s/%s;create=true", databaseDirectory.toString(), databaseName);
Using System.getProperty("user.home") is a nice way of abstracting the file-dependent nature of system paths. The use of Java's Paths abstraction is also another step in the right direction to decouple the application from the underlying file system. So the steps you have take so far are good, and may be sufficient for your use case.
However, there are multiple ways you can make the derby.system.home variable available to your application, and they all vary in terms of pragmatism and simplicity. I could not possibly describe all of them, but I would suggest also trying out a configuration file that you can load using different mechanisms - from the file system, from the classpath, or even from a remote server etc.
I am migrating a legacy project to a new server. Previously the project used a Oracle DB but now i want it to use Postgress. The queries are simple enough and work the same in Postgres.
However the project is missing a Postgres jdbc-driver. Can i somehow add this dependency sideways to the jar without recompiling?
Can i somehow add this dependency to the jar without recompiling?
It depends.
If you are running the server as java -jar myserver.jar ..., then you will at least need to modify the manifest in the JAR file. Strictly speaking this doesn't entail recompiling, but you do need to explode, modify and repack the JAR file.
If the server uses Class.forName to explicitly load an Oracle Driver class, then you will need to change that code to load the Postgres Driver class instead. (There are other ways to use JDBC that avoid this, but this depends on how your legacy server is implemented.)
If your server uses Oracle specific database classes, or Oracle specific SQL features (or it needs to do the same in the Postgres world) then more extensive changes will be required.
But without actually examining your codebase in detail, we can't predict what is required.
My advice is to replace the Oracle driver JAR with a Postgres driver JAR, and see what happens when you run your server against a Postgres database with the appropriate schemas and data.
But I wouldn't do this "in production". Do it in a test environment. If you can't set up a suitable test environment ... forget it.
And if you don't have the source code for your server, I would forget it too. If anything goes wrong you will most likely need source code to figure out the problem and fix it.
I've been messing around with Apache Derby inside Eclipse. I've booted up a Network Server, and I've been working with servlets. In my Eclipse project, I have a class called "User", inside the package "base.pack". I have an SQL script open, and I've been trying to convert User, which implements Serializable, into a custom type. When I run the following lines, everything works fine:
CREATE TYPE CARTEBLANCHE.bee
EXTERNAL NAME 'base.pack.User'
LANGUAGE JAVA
This follows the general format they identify here: http://db.apache.org/derby/docs/10.7/ref/rrefsqljcreatetype.html#rrefsqljcreatetype
Now, when I try to create a table using this new type, I get an error. I run the following line:
CREATE TABLE CARTEBLANCHE.TestTabel (ID INTEGER NOT NULL, NAME CARTEBLANCHE.bee, PRIMARY KEY(ID));
And I receive the following error:
The class 'base.pack.User' for column 'NAME' does not exist or is inaccessible. This can happen if the class is not public.
Now, the class is in fact public, and as I noted before, it does implement Serializable. I don't think I'm stating the package name incorrectly, but I could be wrong. I'm wondering, is this an issue with my classpath? If so, how would you suggest I fix this? I admit that I do not know much about the classpath.
Thank you.
(For reference, I have configured my project build path to include derby.jar, derbyclient.jar, derbytools.jar, and derbynet.jar, and I have put these files into my project's lib folder as well).
As politely as I can, may I suggest that if you are uncomfortable with Java's CLASSPATH notion, then writing your own custom data types in Derby is likely to be a challenging project?
In the specific case you describe here, one issue that will arise is that your custom Java code has to be available not only to your client application, but also to the Derby Network Server, which means you will need to be modifying the server's CLASSPATH as well as your application's CLASSPATH.
It's all possible, it's just not a beginner-level project.
To get started with customizing your Derby Network Server, the first topic involves how you are starting it. Here's an overview of the general process: http://db.apache.org/derby/docs/10.11/adminguide/tadmincbdjhhfd.html
Depending on how precisely you are starting the Derby Network Server, you'll possibly be editing the CLASSPATH settting in the startNetworkServer or startNetworkServer.bat script, or you'll be editing the CLASSPATH setting in your own script that you have written to start the server.
If it's a tool like Eclipse or Netbeans which is starting the Derby Network Server, you'll need to dig into the details of that tool to learn more about how to configure its CLASSPATH.
And if you've written a custom Java application to start the Derby Network Server (e.g., as described here: http://db.apache.org/derby/docs/10.11/adminguide/tadminconfig814963.html) then you'd be configuring the CLASSPATH of your custom application.
Regardless, as a basic step, you're going to want to be deploying your custom Java extension classes in the Derby Network Server's classpath, which means you'll want to build them into a .jar file and put that .jar file somewhere that the Derby Network Server has access to, and you'll want to make that build-a-jar-and-copy-it-to-the-right-location process straightforward, so you should integrate it into whatever build tool you're using (Apache Ant?).
And, you'll need to consider Java security policy, because the default security policy will prevent you from trivially loading custom Java classes into your Derby Network Server as that would seem like a malware attack and the Derby Network Server is going to try to prevent that. So study this section of the Security manual: http://db.apache.org/derby/docs/10.11/security/tsecnetservrun.html
I'm using Hibernate 4.3.5-Final for my Java Swing application and I do many UDPATE, INSERT and DELETE with it (in HQL or with Criteria).
Now, what I'm trying to do is to export a SQL script of all modifications done on my database but I don't know how to do that. This script need to contain only the modifications (and not the creation of the tables) and put them on a .sql file (exported file path will be chosen by the user)
Do you have any ideas that can solve my problem ?
Thank you in advance !
[Edit] : Some forums talk about p6spy, can it answer to my problem ?
p6spy should help here.
In general, following should do the job for you:
enable p6spy in your app (see official docs: http://p6spy.github.io/p6spy/)
afterwards you have basically 2 options:
use provided: BatchFileLogger, enabling it via: appender=com.p6spy.engine.spy.appender.BatchFileLogger in spy.properties (it's however undocumented yet, see: https://github.com/p6spy/p6spy/issues/119)
OR
implement custom com.p6spy.engine.spy.appender.MessageFormattingStrategy, that would be returning sql only (see: https://stackoverflow.com/a/23521623/1581069 for idea on implementation) and configure it in spy.properties via: logMessageFormat=FooFormat
set sqlexpression to match the queries you need - restricting CREATE/ALTER/... TABLE/SEQUENCE/... (see official docs: http://p6spy.github.io/p6spy/2.0/configandusage.html)
still there are some tricky points, like:
databaseDialectDateFormat property (to be able to kind of replay the output without modifications). For inspiration for some of the common databases, see the unit tests of p6spy itself: https://github.com/p6spy/p6spy/tree/master/src/test/resources/com/p6spy/engine/spy
I want to use HSQLdb for my Spring-based web application, and I want to have the data persistent (so in-memory is out). According to the hsqldb documentation, I can use variables in the connection URL - but the document doesn't say how to set these.
I've read this question, and there are some interesting tactics, such as creating a listener to set properties when the context loads.
This question explains how to get an appropriate location, so I've combined the two approaches. I've tried using a listener to set a new property with the path lifted from javax.servlet.context.tempdir (it's a File), I've tried using that property directly. Neither approach works.
One punter has commented that one could set user.home via a listener, then use ~ in the HSQLdb URL. I've not tried this, but even if it does work, it strikes me as a little yuck, as I redefine something I potentially don't want to. I'm looking for the "proper" way. How do I set that which the HSQLdb calls "web application properties"? (Or is there one that would be suitable?)
From the documentation:
If the database URL contains a string in the form of ${propname}
then the sequence of characters is replaced with the system property
with the given name. For example you can use this in the URL of a
database that is used in a web application and define the system
property, "propname" in the web application properties. In the example
below, the string ${mydbpath} is replaced with the value of the
property, mydbpath
From reading the source code, System.setProperty(key, value)
Kicking myself - my issue was first an ancient version of HSQLdb then it wasn't unloading - a restart of Tomcat, and everything started working.