Spring Hibernate SessionFactory.getCurrentSession in multithreaded environment - multithreading

I have written a batch application which spawns multiple threads to read assigned files and save records to database. The architecture uses Spring context and Hibernate.
Transaction is managed by Spring and I am using SessionFactory.getCurrentSession to get a session to perform a save operation for each thread.
Consider that I have a generic DAO that handles get, save, update operations and a facade to hide Hibernate implementation, how can I be assured that two threads when invoking SessionFactory.getCurrentSession() are getting their dedicated Session object to perform DB operations.
I found a post in StackOverflow where someone recommended not to use current_session_context_class=thread when using spring managed transaction. what is the default implementation used by Spring for current_session_context_class property?
Thanks in Advance!

As of Spring 2.0 Spring integrates with hibernate throuhg its own implementation(s) of the CurrentSessionContext interface provided by hibernate.
By default spring sets this to the SpringSessionContext to integrate properly. In general you don't want or need to mess with the current_session_context_class unless you are using JTA (although when using Hibernate 4 with a recent Spring version it should also just work).

Related

Does Jooq integrate with JPA Transactions?

I'm trying to figure out Jooq 3.17 transaction semantics when used in a Spring Boot application that already uses Hibernate. I read the Jooq docs on this: https://www.jooq.org/doc/latest/manual/sql-execution/transaction-management/, and I think these are the relevant portions for me:
There are essentially five ways how you can handle transactions in
Java / SQL:
You can issue vendor-specific COMMIT, ROLLBACK and other statements directly in your database.
You can call JDBC's Connection.commit(), Connection.rollback() and other methods on your JDBC driver, or use the equivalent methods
on your R2DBC driver.
You can use third-party transaction management libraries like Spring TX..
You can use a JTA-compliant Java EE transaction manager from your container.
You use jOOQ's transaction API.
By default, jOOQ ships with the org.jooq.impl.DefaultTransactionProvider, which implements nested transactions using JDBC java.sql.Savepoint. You can, however, implement your own org.jooq.TransactionProvider and supply that to your Configuration to override jOOQ's default behaviour. A simple example implementation using Spring's DataSourceTransactionManager can be seen here:
Does this mean that if I'm using Jooq within a Spring Boot + Hibernate application, I need to implement my own version of the example SpringTransactionProvider? Or am I covered by bullet point 4 - maybe Hibernate provides a JTA-compliant Java EE transaction manager?
Is there Jooq logging I can turn on that will show me when Jooq recognizes transactions starting and stopping? Something along the lines of this: How to log the start and the completion of DB transactions in Hibernate but for Jooq?
Does this mean that if I'm using Jooq within a Spring Boot + Hibernate application, I need to implement my own version of the example SpringTransactionProvider?
Not necessarily. It just means you should provide jOOQ with a transaction aware DataSource of some sort, and not use jOOQ's own transaction API.
Is there Jooq logging I can turn on that will show me when Jooq recognizes transactions starting and stopping?
Unless you use jOOQ's transaction API, jOOQ doesn't really care about your transaction. It just works with your transaction aware JDBC resources, just like when you use JDBC directly. There's no jOOQ magic going on here, and thus, there's no jOOQ transaction logging available, as jOOQ doesn't know anything about such transactions.

does a reader for spring batch have to be an injected bean?

The transaction model in Spring batch is not so easy to understand, therefore I wonder where spring batch really needs to have proxies to fulfill all its work.
So does Spring need to create proxies around Readers, Writers, Processors and Steps and therefore I have to inject them as beans? or is it OK to no use injection for them?
Spring only uses proxies within batch when you're using step scope. Otherwise, the beans are created and injected. With regards to not using injection for them, you can take that approach but there is a lot going on under the covers that you'd need to handle yourself so it's not recommended.

Using EHCache in JSF Web Application

I am working on JSF application. I want to cache some database table on periodic basis. I heard EHCache does it, but I only see its use in Hibernate. Is it possible to use EHCache in JSF application or is there some alternative for that in JSF?
if you want use EHCache in front end part you should write cache handler with
net.sf.ehcache.CacheManager or primefaces support now ehcache, if application is big, prefer to have
cache handler service which put/get/check objects from the cache.
It is your responsibility to integrate with a caching layer if none of the frameworks you use provide such an integration. You will usually do this at the service layer that computes your view objects from your stored domain objects.
Ehcache supports different caching patterns. And here again, picking the one that fits your use case is your responsibility.

Global Transaction Handling in Spring Integration

Its a Spring Integration application.I have a requirement where I need to persist to DB and then post to Queue/Topic. This should be part of a single transaction. I am planning to use the JTATransactionManager. Application Server is Tomcat. Would someone please provide some sample configuration code required for this implementation.
You need a third party stand-alone XA transaction manager such as Atomikos. Tomcat doesn't have one.
You might also consider alternatives to using full-blown 2pc.

What technologies are best for my application: Struts with Hibernate or Spring with Hibernate

I have a working knowledge of Struts2 and Spring. I want to develop an application that manages information for multiple companies. I am totally confused about what technologies are best for my application. For instance: Struts2, and Hibernate MVC with Spring.
Can somebody help me select appropriate technologies?
Here is a quick breakdown of a J2EE stack you can use:
Use Struts2 for your controller layer
Use Hibernate for your data abstraction layer. Create service interfaces for your DAO. The interfaces will allow you to use some type of RMI for services later if desired, meaning those services can run on different machines than your web app. Have concrete classes implement those interfaces. The classes will contain business logic and validation of data, and will wrap the Hibernate session. The Hibernate session is used to read/write to/from the database. Use Hibernate annotations to expedite the implementation of Hibernate beans.
Use Spring for instantiating your service classes and Struts2 actions. Configure Spring to inject service instances into your Struts2 actions. This is called dependency injection. Reference interfaces, not classes in your Struts2 action's setter methods for the DI.
Use the Struts2 tag library or JSTL in your JSP, which will be your view layer.
Use Maven for your builds and deploys.
Run Apache with mod_jk, and use Tomcat as your servlet container. mod_jk runs w/ the Apache process, and passes requests to the Tomcat servlet container, which lives in the JVM.
If your application requires search capabilities, use SOLR, a REST service built on top of Lucene.
Instead of using Struts2, you could also take a look at Apache Wicket.
I had the same question few days back and following are the links I used to make a decision - I settled for Spring MVC. Also check out Spring ROO if you are starting afresh.
Choosing the right web framework
Comparing web frameworks
What Web Application Framework?
Ultimately choice will be based on your needs - but above links discuss what parameters you should consider before choosing one.
Hope that helps.
Agree with #Simian, and add some comments and reasons.
From a technological perspective, you should use any framework that utilize modern and mature technologies, such as Struts 2, Spring MVC, Hibernate, JSF, and etc.
However, from a business perspective, you should take more emphasis on the business model that your project consist of, and the demand for the framework is easy and rapid to implement, as well as robust and easy to maintain.
Therefore, as you are familiar with Struts 2, and Spring, I recommend:
1, Use Struts 2 as the MVC framework of your project, but use AJAX if required. You can also develop your interceptors to fulfill some common requirements of your project.
(Or, if you have time, you can learn Spring MVC as it works well with Spring framework, and has better support of AJAX and RESTful. JSF is not recommended, not because it isn't a superb framework, but it use a set of complete different concepts comparing to Struts 2 and Spring MVC, and it is difficult for an unskilled person to debug )
2, Just use Spring jdbcTemplate as your data layer, use DAO pattern to decouple.
(Or , you can learn Hibernate or JPA as your ORM framework, if you have time.)
3, Use Spring IoC to manage your objects and integration with Struts 2 and Hibernate, and manage transactions with Spring's annotations.

Resources