How to obtain new hibernate transaction inside AsyncExceptionHandler? - spring

I have 2 DB instances, say DB1 and DB2.
I need to write data to a table in DB2 asynchronously (#Async block).
If there is an exception while writing data to DB2, we need to write the data to another table in DB1.
I am using custom exception handler which implements AsyncUncaughtExceptionHandler (provided by Spring AOP) to catch the exception and do the operation 3. However, I am unable to do the transaction as there are two DB transactions involved and I loose the transaction. I get an error saying org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
I am using Spring 5.0.3 and Hibernate 5.2.11.

Related

Oracle database transaction

I have a question about transactional queries on oracle Db(or other RDBMS).
I have for instance 3 tables in my transactionnal script. What happen if I call begin transaction on each of three tables and at the end I command commit also on each table?
Is it necessary to do that?

Hibernate insert and call store procedure to read non persistent data

I have a method with transaction annotation that persist an entity and then do a call to an oracle function who read this non persistent data but the function never see the data inserted in the first step. I'm doing this because is function fail then a rollback must be done. Another thing is that the function update a different database.
Any help about how to do this?

Hibernate JPQL Update/Delete operations inside a transaction

Using Hibernate (4.2.7.SP1), Spring and Oracle. Noticed that when the last line in the method (JPQL UPDATE) is executed, but before the #Transactional methods ends, dev's name A is commited to the database and it is visible(in selects from another connection)!
#Transactional
public void doInTransaction()
{
User user = userDao.findById("dev");
user.setName("A");
userDao.getEntityManager().createQuery("UPDATE User set name='B'").executeUpdate();
}
Note that User is a subclass of Person, InheritanceType.JOINED, i.e. there are two tables involved, the name field is inherited from Person.
Found some information here http://in.relation.to/Bloggers/MultitableBulkOperations explaining how hibernate performs the UPDATES and DELETES, and that for inheritance tables it creates temporary tables prefixed with HT_.
Performed some debugging, the issue as I see it can be represented in two lines:
update ILC_PERSON set name = 'A';
create global temporary table HT_ILC_PERSON_USER (id varchar2(255 char) not null) on commit delete rows;
-- bellow should execute the JPQL UPDATE User set name='B'
What happens is that when the DDL for creating the temporary table is executed, oracle commits automatically the previous DML.
Question:
is this a Hibernate bug?
is there some misconfiguration in the project (using LocalContainerEntityManagerFactoryBean with JpaTransactionManager)?
does this simple means we cannot use JPQL UPDATE/DELETES for entities with InheritanceType.JOINED in one transaction?
something else?

H2 Database triggers

generally, when you use H2 database you have to create custom class and implement method "fire" to write Trigger.
For my project I am using batches for inserts. I need to use trigger to make a kind of complex data integrity check on the table I want to insert into, which is not possible using CHECK. So I have to make a select statement in the trigger method to make a check.
Since there could be many inserts I would like to avoid many server roundtrips for each trigger select statement(that's why I am using batches for inserts). Does H2 database sends request to DB from "fire" method everytime if I make a select statement there or is this trigger method somehow integrated into database engine itself ?
Thanks,
Lubos
Triggers are executed on the server side, so the are no server roundtrips when executing triggers.

Is there a limit to DB2 JDBC transaction size?

I need to insert a large number of rows (up to 100,000) into 6 different DB2 tables. I am using Java JDBC to do it. I would like to do it all in a single database transaction so that the whole thing can be rolled back if any issues are encountered. Is there a limit somewhere (in JDBC driver or in DB2) to how many inserts can be handled in a single transaction? We are using DB2 version 8.
The size of a single transaction is limited by the size of the database transaction logs. With a sufficiently large transaction log you can do what you are asking.
You don't say what platform you are running DB2 on, but for Linux/UNIX/Windows the transaction log size is controlled by three database configuration parameters - LOGFILSIZ (the size of each transaction log file), LOGPRIMARY (the number of primary transaction logs) and LOGSECOND (the number of secondary transaction log files).

Resources