How can I write entity history records to another server? - hibernate-envers

I want write entity to server1, but entity history to server2, how do I config my datasource?
Did anyone particular it?

There is no simple DataSource configuration that you could use.
The DataSource would need to analyse SQL statements in order to decide to which server to send them. You probably could implement something like this yourself, by searching for the names of the history tables in the SQL statements, but I wouldn't recommend that.
Assuming your database supports this it seems more appropriate to use database link or some comparable technology, which allows you to write to the same database but have the data actually end up on the second server.

Related

Using variables for schema and table names in a Redshift query

I want to be able to use the variable names in Redshift which refers to my DB Objects (like schema and table names). Something like...
SET my_schema="schema":
SET my_table="table";
SELECT * from #my_schema.#my_table;
But looks like Redshift doesn't have such feature. Is there any workaround possible to achieve this?
There are a few ways you try to attack this. But first trying to use a database engine for functions beyond querying the database is a waste of horsepower and the road to db lock-in. So I'm going to focus on ways to do this before the database.
The most complete way is to use a front-end system that clients connect to and then this system in turn connects to the db. The one I've used in the past is pgbounce-rr which pools connections to the the db but also allow for modifications to the SQL before being sent on. This will do what you want but you will need a computer to perform this work.
If you use Redshift data-api you could put a Lambda function in series which performs the SQL modifications you desire (but make sure you get your API permissions right). However, I expect it is unlikely that you are looking to move to an API access model.
Many benches support variable substitution and simple replacements in the SQL can be done by the bench. However, this is very dependent on which bench you use and having all users' benches configured correctly.
Bottom line - if you want something to modify your SQL do if before it goes to Redshift.

Is there Entity Framework support for saving to multiple databases on the same SQL Server in single transaction?

I have two databases on the same SQL Server instance. I would like to write a record to each database in a single transaction.
In Linq-to-SQL, I would connect to either database with one context and use three part naming to identify the tables.
Is there a similar capability in Entity Framework?
I'm trying to avoid DTC, it has been forbidden - so the usual TransactionScope approach is not available to me.
There is not a way I know of... you could potentially use the UnitOfWork pattern
http://www.codeproject.com/Articles/581487/Unit-of-Work-Design-Pattern
That might allow you to at least go back to the other Db and un-commit?
Personally I think your going to struggle.

Can EF successfully be used to create very large tables?

We are very successfully using EF 5.0 for our real time server as well as from our internal websites. Now I need to create a utility that parses the data history to create a new table, which will be done using a copy of the production database used for data mining. Given that EF is transaction based, is there a good way to create a very large table where the table may have > 1M rows? My current thinking is no, and that the way to do this is to perhaps read the data with EF, but create a CSV file that is then bulk loaded, which I successfully do in some other situations. I'm not necessarily looking for the most efficient way, but I cannot imagine that EF or SQL would do well to add > 1M records with a single transaction. I know I could batch them in 1000 record chunks but that is not especially appealing. EF is said to be MSFT's principal data access technology going forward but they need to support this sort of scenario as part of that plan. Any ideas and insight appreciated. Thx.
EF is not geared for bulk operations (see Efficient way to do bulk insert/update with Entity Framework).
Instead of using a CSV to bulk-load, perhaps you want to look into the SQL Server Bulk Copy API

libpq code to create, list and delete databases (C++/VC++, PostgreSQL)

I am new to the PostgreSQL database. What my visual c++ application needs to do is to create multiple tables and add/retrieve data from them.
Each session of my application should create a new and distinct database. I can use the current date and time for a unique database name.
There should also be an option to delete all the databases.
I have worked out how to connect to a database, create tables, and add data to tables. I am not sure how to make a new database for each run or how to retrieve number and name of databases if user want to clear all databases.
Please help.
See the libpq examples in the documentation. The example program shows you how to list databases, and in general how to execute commands against the database. The example code there is trivial to adapt to creating and dropping databases.
Creating a database is a simple CREATE DATABASE SQL statement, same as any other libpq operation. You must connect to a temporary database (usually template1) to issue the CREATE DATABASE, then disconnect and make a new connection to the database you just created.
Rather than creating new databases, consider creating new schema instead. Much less hassle, since all you need to do is change the search_path or prefix your table references, you don't have to disconnect and reconnect to change schemas. See the documentation on schemas.
I question the wisdom of your design, though. It is rarely a good idea for applications to be creating and dropping databases (or tables, except temporary tables) as a normal part of their operation. Maybe if you elaborated on why you want to do this, we can come up with solutions that may be easier and/or perform better than your current approach.

JPA insert statement

What's the correct syntax of a JPA insert statement? This might sound like an easy question but I haven't been able to find an answer.
I know how to do it from Java code but I'm looking for a way to insert objects into the database if the database was created.
Any ideas?
There is no INSERT statement in JPA. You have to insert new entities using an EntityManager. The only statements allowed in JPA are SELECT, UPDATE and DELETE.
Here is a good reference on persisting JPA objects using an EntityManager. As an example, this is how to insert objects using the persist method:
EntityManager em = getEntityManager();
em.getTransaction().begin();
Employee employee = new Employee();
employee.setFirstName("Bob");
Address address = new Address();
address.setCity("Ottawa");
employee.setAddress(address);
em.persist(employee);
em.getTransaction().commit();
If you want to insert data to the database outside java you need to use native SQL. Use SQL Standard to make sure most databases can execute the script. When the application runs, JPA will make the mapping of the new data and convert it into objects when needed.
How to make sure the script works in all databases? well thats the same problem any DBA has when making Store Procedures or native queries... thats why JPA exists, to avoid making it directly in SQL, but I know sometimes is needed that way.
I suggest you to make 3 main scripts. One for Oracle, one for SQL Server (there are some issues in the date datatypes from 2005 to 2008 versions so be careful) and one for MySQL. Start your script with standard SQL and when you test it in this databases you will find some fixes you will need to do for each DBMS.
One you got it you can make a file script (*.sql) file and run it with the DB manager. If it works run the server, put the app online and the data will be integrated just fine.
The option that looks more promising so far is using Flyway. It is a more automated way of doing it and handles the upgrade process of databases basically automatically.
No need to write a separate INSERT query in JPA. JpaRepository has inbuilt saveAndFlush() method which you can use to insert into the database. Hope this works for you.