EnableTransactionManagement for multiple databases in Spring - spring

I have an application that needs to access two databases. I am trying to use Spring transactions to accomplish this.
1) Since I have two databases and a transaction manager takes a datasource as a parameter, must I configure two transaction managers, with each #Transactional specifying the correct transaction manager to use? ex: #Transcational("database1"), #Transactional("database2").
2) Since #EnableTransactionManagement will look for a single transaction manager to use for all transactions, I do not think I can use this annotation. Is that the case? Can I still utilize transactions with #Transactional("database") and no #EnableTransactionManagement?

Please look to the relevant documentation: http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html#tx-multiple-tx-mgrs-with-attransactional

Related

What is the point of using spring transaction while we have database level transaction?

What is the point of using spring transaction while we have database level transaction ?
If you use mysql, oracle or any other db in java, they provide methods to make things inside transaction then why spring transaction if I can do transaction using java DB driver methods ?
It's another layer of abstraction over the database transaction API. So if you wanted to use multiple databases with global transactions, Spring would allow you to do this. While I have never done it, I believe it would allow you to use DB2 and Hibernate together, for example.
Generally, what I've found is, if a feature is available in Spring, it's because there is a use case for it. They don't just put things into the framework without a reason.
Also, Spring provides declarative transaction demarcation, which produces more readable and maintainable Java code. The declarative approach lets us change the transaction strategy easily, without changing the code.
The Declarative transaction management approach allows you to manage the transaction with the help of configuration instead of hard coding in your source code. This means that you can separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions
We used Spring AOP along with Hibernate using this transaction strategy Here is an example, Spring AOP transaction mangement with Hibernate.

Making specific method non transactional in Spring

I have a spring application which is based on Spring Batch. By default spring batch introduces transaction for its steps (i.e. at reader,writer and processor) . There are certain stages where I don't really need transaction to be enabled. Because transaction is enabled unnecessary for these methods its giving me some runtime errors as I am making call to two different databases in one method.
Is there any annotation which spring provides to DISABLE transaction for a specific set of methods ?
OR is there anything available in spring batch which can allow me to get rid of transaction either completely or declarative
I am even open to the solution which can disable transaction globally.
Any link , paper will greatly be appreciated.
Thanks in advance
Samir
Spring Batch is inherently transactional. Even if your datasources are not transactional, the semantics of the JobRepository require it. The closest you can get with Spring Batch and not being transactional is using the ResourcelessTransactionManager. This transaction manager is essentially a no-op transaction manager that just keeps track of if an operation is within the scope of a transaction or not.

declarative transaction management in spring 3.1+

I am a newbie to spring hibernate.
And I find 2 ways to handle transactions in Spring declaratively - ProxyFactoryBean using TransactionInterceptor or the #Transactional annotation.
How do we decide which one to prefer?
Is there any other way available for declarative transaction management too?
Advantages of annotaions way:
Annotations are directly visible in the code.
Advantages of xml way:
You can reuse the same conf between multiple beans
You can share some class between two applications and apply different transaction rules
I prefer annotations where it is possible. It saves a lot of time when you read the code (you do not need open one more file and check it periodically).
Other way for declarative transactions: use <aop:config> with <tx:advice>. See corresponding entry in official documentation. It is a variation of xml way that is more easy to do then ProxyFactoryBean (you do not need to wrap beans / declare transaction interceptor manually).
Hope this helps.

Can I use two different entity managers that point to the same datasource in one transaction?

I'm using spring with JPA. And I've got two EntityManagers that contain different entities (different modules of one application), and both point to the same datasource.
Can I use both of them in one transaction (using single TransactionManager) ?
Do I have to use JTA for that ? If so what is the best option to use it under tomcat ?
How do I configure it in spring ?
Both standard JPA transactions and Spring's JpaTransactionManager are bound to a single EntityManager. They cannot talk to multiple managers, and so cann't coordinate a transaction across them.
If you need to do this, you either need to merge your entity manager configs so you have just one EntityManager, or use JTA transactions (via Spring's JtaTransactionManager).
If you're using Tomcat (which has no out-of-the-box JTA support), then you'll need to find a third party JTA implementation.

spring 3 configuration without transaction manager

How to not use (disable) transaction manager in spring configuration?
I'm doing simple app that will add rows to database, so no need in any transactions.
So, is it possible configure in xml, not to create any transaction managers?
PS: i use hibernate if this matters anyhow.
If you don't configure a Transaction Manager, neither will Spring. Just don't add any <tx:> tags and don't configure any PlatformTransactionManager beans (in your case HibernateTransactionManager).

Resources