jdbc - transaction manager needed by our web server for db transactions? - jdbc

I'm using Jetty with mysql. I need some basic transaction support, and jetty is warning me at startup that no transaction manager is in use. I thought transactions were native to mysql? I'm trying something like:
Connection conn = ...;
conn.setAutoCommit(false);
// insert into table foo some data
// insert into table grok some data
conn.commit();
If an exception is thrown between the two statements, I see that data has made its way into table "foo", so the transaction calls did not work.
So I guess we really do need a transaction manager here, am I understanding this correctly? If so, I was looking at bitronix: http://docs.codehaus.org/display/BTM/Home
Thanks

Transactions are not "native" to MySQL (unlike other databases).
You need to make sure you are using the InnoDB storage engine, otherwise you won't be able to make use of transactions.

Related

jdbc is not able to find data flushed by jpa repository in the same transaction

I have a JUnit 5 testcase which is annotated with #Transactional and it is calling save service (which uses JPA saveandFlush) first, and trying to retrieve the same data/entity by using find service, which is using plain JDBC for searching, but it is not able to find that entity.
When I tried using isolation of the transaction as read_uncommitted it threw exception saying "java.sql.SQLException: READ_COMMITTED and SERIALIZABLE are the only valid transaction levels" please note that I am using Oracle database.
Is there any other way we can read the data which is present in same transaction by JDBC code?
Oracle does not implement Read Uncommited isolation level, so you will not be able to see uncommited changes from other sessions.
As Mark Rotteveel and Marmite Bomber said, two different transactions/connections(JPA and JDBC) can not see each other's uncommitted data, specially for application having Oracle database.
I had to use spring managed jdbc template so that JPA and the JDBC template uses the same transaction and data is visible to each other.

Hibernate/JPA vs JDBC performance

We are using JPA/Hibernate for project where we need to insert, say 10000 records in database with multiple joins/relations etc. The functionality is fine but the performance is really slow.
Just wondering if migrating to JDBC will help in some performance gain?
Thanks,
-csn
You can do batch inserts with JPA/Hibernate - see "Batch processing".
You would almost certainly get better performance by doing batch inserts in JDBC (addBatch(), etc), but the Hibernate method may be more desirable if your schema is complex.
If you use JDBC batches, make sure that you do all of your addBatch() calls in a transaction.
If you happen to be using MySQL, be sure to add rewriteBatchedStatements=true to your connection parameters.

Clojure JDBC transaction not rolling back on BatchUpdateException in HSQL

I'm writing a Clojure program using clojure.java.jdbc. I'm using DBCP to pool connections to HSQL 2.2.8. I have a (transaction) block in which I test if a schema exists, and if not, creates it and a bunch of tables. One of the statements after the schema create (I believe a MERGE statement) throws a BatchUpdateException.
The issue is that the schema create is not rolled back on the BatchUpdateException, even though they're part of the same (transaction) block.
Are there known issues with Clojure JDBC interacting with DBCP or HSQL?
Never mind.
Transactions don't apply to schema changes, apparently. WTF?

Dynamic data source routing in spring

I've single DB with three identical schema in PostgreSQL.
Now I need to select particular schema for DB operation based on locale-key (store in user session). I found somewhere that this thing is similar to dynamic data source routing.
Anyone have any idea about How to implement this in Spring?
Will this effect transaction management in anyway?
Please do share any sample code if possible.
Any suggestion would be appreciated.
Thanks & Regards.
I suggest using this approach:
First define a locale interceptor either session-based or change interceptor in your Spring MVC configuration.
You can now use LocaleContexHolder to fetch the current locale on the attached thread.
Use the reference blog post to define your dynamic routing data source. The parameter on your data source router will come from locale. Use LocaleContextHolder to determine the locale then use it to determine which data source should be used.
If you have single DB, then the dynamic aspect should not be related to connection pooling - all connections are still for single database. All you need to do is to dynamically set proper schema after starting the transaction.
This may be achieved using some aspect with order higher than <tx:annotation-driven />. In this aspect you should acquire current connection:
DataSourceUtils.getConnection(dataSource)
and issue the following PostgreSQL statement (see: http://www.postgresql.org/docs/9.1/static/sql-set.html for information about schema parameter);
set schema 'schemaname-on-the-basis-of-session-parameter';
See also using schemas in postgres.
As for the transaction management - transactions are related to physical connections and sessions. Schemas on the other side are kind of namespaces, so you don't have to change transaction management, just set current schema at the beginning of each transaction during user request processing.

GORM (Hibernate) Interceptor to run certain SQL before Hibernate runs it in the db in a Grails application?

I have a grails application in which I am using GORM. This works great. However I have a requirement where, before any SQL is called in the database, it has to run a certain stored procedure. So, is there a way I can do something that will trigger my method that kicks off a stored procedure before Hibernate runs the SQL for select, insert, update, delete etc.
Your response will be greatly appreciated.
(P.S.- The reason I have to run certain stored procedure is to change Oracle Workspace)
There might be several options.
You could use a dataSource wrapper that extends org.springframework.jdbc.datasource.DelegatingDataSource and runs the statement whenever getConnection is called.
I guess it's ok to call the statement (stored proc) only once per transaction, so DelegatingDataSource is probably the most approriate solution.
You could also use http://www.grails.org/plugin/jdbc-pool plugin (wraps Tomcat JDBC Pool) and try to hook to the datasource pool implementation. Tomcat JDBC Pool supports interceptors.
It's also possible to modify the SQL sent by GORM/Hibernate using a Hibernate interceptor, if that helps: Is it possible to map a table name for a domain object dynamically in grails? .
It's not clear whether you want to run this stored proc once before any SQL is executed, or before every SQL statement is executed. Here's a suggestion for both cases:
Once Only
Call the method that invokes the stored proc in Bootrap.init()
Before Every
Call the method from the before* GORM event handlers

Resources