struts2 and spring transactions - spring

I have a app written with struts2 and spring and JPA over hibernate. Form spring i use transactions and IoC.
My app is structured in 2 layers : Struts Actions and some "services" classes that are dealing with CRUD operations, classes that are annotated with #Transactional.
If in one of these services classes method an exception occurs i have to throw that exception from this class(to struts action level) in order for Spring to rollback transaction automatically, right?
What will happen otherwise (if a don`t throw that exception and the transaction will never rollback) ?
PS. i use spring managed EntityManager for all DB operations.
Thanks

Related

No existing transaction found for transaction marked with propagation 'mandatory'

This is somewhat weird behavior. I moved from Spring MVC to Spring Boot Configuration. This has caused the #Transactional method to throw the above error. Not sure why would switching from MVC to boot mvc do that. Anyone having nay idea about this?
I don't have specific code snippet as this is application wide and was working fine till I had Spring MVC. Now I moved to Spring Boot (Dependency upg. Filters moved etc.)
Flow for the methods having #Transactional is:
Action is called
If contains execute() which calls a Save method with #Transactional from a class which is implementation of an interface.
This class have another Dao method having #Transactional.
Thanks

How to register a hibernate typedescriptor in a "classic" Spring 5 application with jpa entitymanager

I have a classic spring 5 application (NO spring boot). For a custom basic type i have created a typedescriptor by subclassing AbstractTypeDescriptor according to Hibernate 5.2 Documentation - 2.3.5. Explicit BasicTypes. I am using hibernate via an jpa entitymanager. I have two spring beans configured for that: a LocalContainerEntityManagerFactoryBean and a HibernateJpaVendorAdapter.
Now i am a bit lost, how to register the typedescriptor during startup of the application. The docs mention an approach using a hibernate configuration object. But i have no idea, how to get this? Any help appreciated!

Does spring-data by default always use the same persistence context within the same request?

An example is if my spring controller has two Autowired services, and both services have a PersistenceContext also controled by spring (and i'm doing nothing more), will both share the same context in every request by default?
No, they will use different ones. A persistence context (EntityManager) is defined to be a thread-bound concept in JPA. Thus each request will see a fresh instance of EntityManager for each new request.
For a singleton Spring component that gets an EntityManager injected, Spring will autowire you a proxy instance so that it can easily exchange the backing instance. This a core Spring container feature and doesn't need anything in Spring Data JPA (see the documentation here).

Mule 3.3 spring 3.1 hibernate 3.6 transaction management

I am working on an application which involves mule, spring, hibernate with annotations. I am using org.springframework.orm.hibernate3.HibernateTransactionManager. Now the problem is :
I have certain components in mule which logs data into db based on conditions using hibernate. I have used #Transactional which inserts few data and then commits the transaction when the method scope is completed. But the behaviour which i want is : first component inserts data based on some condition, but the transaction should not commit immediately, again my second component that is a java class should insert some data again then third etc. if any of the component fails all the queries executed in all the components should be rolled back. all of this components are separate java classes
How can i achieve such behaviour.
thank you,
Let your whole component execution chain be in a transaction. then it will meet your expectation. it is easy to do it if all your components are in the same one spring appliction context. In the case, there are two things you need to do:
Add #Transactional annotation on your component class or specific methods which need to be in transaction. By default, transactional method use REQUIRED Propagation setting which will let all method in the exection chain merge to only one transaction.
Make sure Spring can scan all your components.
#Transactional
#Component( "lbsProviderApiCallJob" )
public class LbsProviderApiCallJoImpl implements LbsProviderApiCallJob, ApplicationContextAware {
If all your component are not in a spring context. it is complex to make it.

What's the difference between using #Transactional and Spring template?

If I use #Transactional in my DAO will all of my EntityManager queries be encapsulated with commit and close? Or do I need to use Spring template (JPA template, Hibernate template)? What's the difference between using #Transactional and Spring template?
The difference between using annotation-based transaction demarcation (#Transactional) and the TransactionTemplate is that TransactionTemplate couples you to Spring's transaction infrastructure and means that you will programmatically handle setting the transaction status if the transaction should be rolled back. You can use annotation-based transaction demarcation with the Spring transaction support or with AspectJ transactions outside of a Spring container.
See also the online documentation for transactions in Spring.
The Spring template classes are only there to provide a nicer API for doing persistence operations - they do not deal with transactions. If you want to have transactional operations, you either need to use the #Transactional annotation approach, or use TransactionTemplate.
When you use #transactional with the proper Spring configuration, Spring will recognize that the method needs an transaction and will handle the transaction creation, commit and close for you.
Like skaffman said, #transactional is not directly tied to the template classes. They can be used for any class that may need transactions.
do u mean usin #transactional will encapsulate my dao methods with commit,close or when using spring transaction template (jpatemplate, hibernatetemplate) ?

Resources