Embedded DBs compatible with SQL Server Data - java

Working on a JAX-RS application which uses Microsoft-SQL-Server as a Database.
It does not use any ORM frameworks, just plain old JDBC.
Most of the application's operations involve store and retrieve data as XML into DB tables.
I have a use case where I have to run this application offline. So there wont be any connection available to DB SQL-Server.
Whilst looking into my options thought I would embed the DB and ship with the application EAR.
Looking into options I learned that, SQL-Server-Compact does not have a proper JDBC driver.
Is there any other In-Memory DB that could serve my purpose?
I want avoid any code changes like: changing the SQL queries (written specifically for Microsoft-SQL-Server).
Is there any solution which I can use and ship my application just by changing the DataSource to embedded DB?
note: I could not find any useful post on stackoverflow for this query, If it's already been discussed. Please point me to the post and I will delete this duplicate question.

Related

Concurrent access to same file using JPA

I have a desktop java application that can be run by different users. It makes use of JPA to access a database that is stored as a file. For this purpose I do not want to run a separate database server.
The purpose of the database is to store actions that are done in the program, it's a simple "store record" operation. But all users must be able to read these stored records.
How can I make sure the different applications can save their actions, while not overwriting the actions of the other? So I need a way to:
Open the database (file)
Lock it
Write the added action
Close the database (file)
Is there a way in JPA to do/enforce this ? I'm now using hibernate, but that's not a strict requirement.
Please do not answer with "you can do this with technology XXX". Please note that I'm concerned with respect to the concurrency issues and how to enforce that the file is opened, and closed again. How can this be done with technology XXX ?
You can try to use SQLite database file. In this case, you can achieve concurrent access to the same file (e.g. database), and in the same time you can use SQLite JDBC driver along with your JPA provider (e.g. Hibernate).
The only disadvantage might be is that, strictly saying, it's not pure Java approach, as proposed JDBC driver has native libraries bundled there, but I wouldn't consider this as an issue.
JPA is an ORM (Object-relational Mapping) specification ; the R in ORM means the same thing than the R in RDBMS. JPA is so absolutely not suitable for flat file persistence systems

Java web application memory handling

I have a Java web application which uses Hibernate for storing data into the database and retrieving them.
The strategy I am currently using is to load everything from the database on to the application at start up, and saving/updating them to the database as the user interacts with the application.
What I have also done is to keep track of Transaction history for each user as part of the business logic. (So this transaction history is all loaded on application start up).
The problem I can see is that I shouldn't load all the transaction history for all the user, because if there are a lot of the Transaction history, and users might not necessarily need to see them, then that could be a lot of memory being used up, so it is not efficient.
I was wondering if there is something similar to what PHP script can do, which is just query the database only when user request to see the transaction history, and so it is not using the server resource. (Asides from query the database) Or what are some suggestions/comments regards to what I am facing right now.
Thank you.
Query Hibernate when you need a given piece of information and let Hibernate manage putting it back to the database. This will allow Hibernate to manage the caching.
Note, that when using Hibernate, you should let Hibernate manage the data completely. Do not add or change data yourself using raw SQL.
If you are using a modern container, you should consider migrating to JPA as it is the standard in Java EE containers, allowing you to be more flexible when you need to scale. JPA is very close to Hibernate, but is an API, not an implementation, so you have more than one to choose from.
why not query hibernate for every request come in and release after response? This is a common approach.

data migration from an embedded javaDB

I am currently working on swign based javaSE application utlizing an embedded JavaDB for storing the data. My data model (and DB ) is fully java based utilizing JPA entities and JavaEE Persistence mechanism. No SQL scripts are used in the creation and management of the DB as everything is done by JPA.
As this application is going to be distributed to clients and clients usually require new features my question is if there is an effective way of getting the DB data and reinserting them after the updated application (with new tables, columns) has been delivered to the client.
You could use dblook (command line tool that comes with JavaDB) for an SQL dump of your JavaDB content and structure (example here).
However, many structural db changes can be done with a non-empty database as well, e.g. adding columns or adding tables.

Switching between embedded Databases in Java with JPA

Im currently working my way towards JPA 2.0 and I start of liking how easy it is to maintain persistent data.
What I'm currently trying to accomplish is using JPA in a basic desktop application. The application should allow me to open embedded databases which are on my file system. I chose H2 databases for now, but I can really live switching to JavaDB or anything else.
What Im trying to accomplish is, that one can open the database file without previously define a persistence-unit in the persistence.xml file.
I can easily define a unit and persist objects, but it needs to be configured first.
I want to write some sort of database browser which allows opening without preconfiguration and recompiling.
http://www.objectdb.com/java/jpa/start/connection
I saw that ObjectDB allows access for this type of PersistenceFactory creation, but I was not able to transfer this example to other databases.
Am I totally wrong with the way I approach this probblem? Is JPA not designed with on-the-fly database access?
Thank you for your help,
Johannes
Not part of the JPA standard. Some implementations may offer their own API to do it. For example with DataNucleus if you go to this page http://www.datanucleus.org/products/accessplatform_3_0/jpa/persistence_unit.html at the end you can create dynamic persistence-units (and hence EMFs), and that implementation obviously allows persistence to the widest range of datastores you'll get anywhere
You can pass a Map of properties to createEntityManagerFactory() call that defines the database connection info, etc. The property names are the same as in the persistence.xml. I assume most JPA providers support this, EclipseLink does.
You will still need to define the set of classes for the database and map them.
If you do not have any classes either, than you could look into EclipseLink's dynamic support,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic
If you want to make a database browser accessing different databases, you can't use a PU/Entity Manager (imo).
You'll need a dialogue asking a user for the IP/Port of the database, the username/password, the database name to access, and the type of database.
Then all you need to do is create a socket, send requests over the socket, and parse the response into a view.
Since both the request and the response are database specific, the user has to select the proper database driver.

Is Spring/Hibernate suitable for a use case that requires heterogenous db connections?

I have an Oracle database that runs a PL/SQl job once every week to copy data from a table in the Oracle database to another Oracle database using Oracle database links. Both systems run on Unix/Solaris. The job is scheduled and executed via the Unix Cron facility.
The remote machine is now being migrated from UNIX to a Windows platform with a MSQL Server database. I have been looking into the possibility of using Oracle database links to connect from UNIX to the MSSQL Server database which will be running on a Windows platform. It is possible but it looks to me like there is a lot of fiddling around to get it to work and there is not a lot of information on how to implement it.
The other solution i am thinking of is to implement the process as a Java daemon process that will run every week. All it will do is connect to the Oracle database read data from some tables, connect to the MSSQL Server database and run some insert commands. I think i will need two kind of drivers for this - i.e. jdbc for Oracle and jdbc for MSSQL Server.
Does Spring provide any facility that will make the above easier to implement and or maintain? (Specifically the multiple drivers and the scheduling part of the requirements)
Is Hibernate suitable for managing the multiple database connections or is that overkill?
Feel free to also suggest a better solution :)
Thanks
Spring has extensive support for scheduling tasks.
Running queries against multiple databases via different drivers is a basic JDBC feature; Spring is not really relevant there - unless you want it to happen in a single (distributed) transaction. Spring does support those via JTA and XA.
This sounds like a perfect application for Spring Batch
You should check these Pages to see what it's about:
Features
Use Cases
Spring Batch will happily accept different datasources with different drivers, but you will probably have to provide some RowMapper objects to create interim Objects.

Categories

Resources