Spring, Maven : Maven tests failing with 2 transaction managers - java

I have written a few test cases for our Spring-MVC based project. As the project is huge, we are using 2 Data-sources and 2 Transaction managers, both talking to the same Database using HikariPool.
When using the webapp, if I save an object with 1st data source and retrieve it with 2nd data source, it's working fine, but not when I am running mvn test.
Any ideas?
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
"file:src/main/webapp/WEB-INF/spring/root-context.xml", "file:src/main/webapp/WEB-INF/spring/appServlet/security-applicationContext.xml"})
#Ignore
#WebAppConfiguration
#Transactional
public class TestingController {
//All autowired dependencies
}
DAO layer where code is failing :
#Repository
#Transactional
public class GroupNotesDAOImpl implements GroupNotesDAO {
#Autowired
#Qualifier(value = "sessionFactory_origin")
private final SessionFactory sessionFactory;
#Autowired
#Qualifier(value = "sessionFactory_extended")
private final SessionFactory sessionFactory_extended;
#Override
public GroupNotes getGroupNoteByIdExtended(int id){
Session session = this.sessionFactory_extended.getCurrentSession();
System.out.println("Id of note is "+id);
// Getting null below, even though the object exists, id is non-zero
return (GroupNotes) session.get(GroupNotes.class, id);
}
}
Inside a browser, everything is working just fine. The extended data source is for long running services.
Save DAO code :
#Override
public int saveGroupNoteAndReturnId(GroupNotes mnotes, int msectionid) {
Session session = this.sessionFactory.getCurrentSession();
if(mnotes.getCanvasId()!=0) {
GroupSection groupSection = (GroupSection) session.get(GroupSection.class, msectionid);
groupSection.getSectionsnotes().add(mnotes);
mnotes.setOwnednotes(groupSection);
int saveId = (Integer) session.save(mnotes);
session.flush();
return saveId;
}else {
return 0;
}
}
Test cases :
// First test calls the save method
groupNotes = TestUtils.createGroupNotes("testtag", "testtext", loggedInUser.getId());
groupNotes.setCanvasId(mcanvasId);
int noteId = this.groupNotesService.saveGroupNoteAndReturnId(groupNotes, msectionId);
assert noteId!=0;
The 2nd test calls to retrieve the object for duplicating. As duplication is a long run process, it's executed with extended transaction manager
int success =this.groupNotesService.duplicateGroupNote(noteId, msectionId, groupId, true,true,4.0,false);
assert(success!=0);
root-context.xml :
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="org.USERNAMEql.ds.PGSimpleDataSource"/>
<beans:property name="maxLifetime" value="30000" />
<beans:property name="idleTimeout" value="40000" />
<beans:property name="connectionTimeout" value="60000"/>
<beans:property name="maximumPoolSize" value="6"/>
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:USERNAMEql://localhost:PORT_NUMBER/DB_NAME</beans:prop>
<beans:prop key="user">USERNAME</beans:prop>
<beans:prop key="password">PASSWORD</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory_origin"/>
</beans:bean>
<beans:bean id="sessionFactory_origin" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.ourapp.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.jdbc.lob.non_contextual_creation">true</beans:prop>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.USERNAMEQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">true</beans:prop>
<beans:prop key="cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
<beans:prop key="connection.release_mode">after_statement</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="extended_transactions_data_source" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="org.USERNAMEql.ds.PGSimpleDataSource"/>
<beans:property name="maximumPoolSize" value="3" />
<beans:property name="maxLifetime" value="30000" />
<beans:property name="idleTimeout" value="40000" />
<beans:property name="connectionTimeout" value="60000"/>
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:USERNAMEql://localhost:PORT_NUMBER/DB_NAME</beans:prop>
<beans:prop key="user">USERNAME</beans:prop>
<beans:prop key="password">PASSWORD</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager_extended"/>
<beans:bean id="transactionManager_extended" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory_extended"/>
</beans:bean>
<beans:bean id="sessionFactory_extended" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="extended_transactions_data_source" />
<beans:property name="packagesToScan" value="com.ourapp.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.jdbc.lob.non_contextual_creation">true</beans:prop>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.USERNAMEQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">25</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">false</beans:prop>
<beans:prop key="cache.use_query_cache">false</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
<beans:prop key="connection.release_mode">after_statement</beans:prop>
</beans:props>
</beans:property>
</beans:bean>

Related

java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager error in Spring MVC while pulling data

I am trying to learn creating ReST API end points using spring mvc and Hibernate without using springboot. When I am running my end point , I am getting internal server error saying that ,
java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager.createQuery(String, java.lang.Class)" because "this.entityManager" is null.
My spring-servlet.xml file under src/main/webapp/web-1nf is like the following,
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.postgresql.Driver" />
<beans:property name="url" value="jdbc:postgresql://localhost:5432/company" />
<beans:property name="username" value="postgres" />
<beans:property name="password" value="postgresql" />
<!--<property name="socketTimeout" value="10"/>-->
<beans:property name="connectionProperties">
<beans:props>
<beans:prop key="socketTimeout">10</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.springmvc.Employee</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop
key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<context:component-scan base-package="com.springmvc" />
<tx:annotation-driven transaction-manager="transactionMgr"/>
<beans:bean id="transactionMgr"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="mgrFactory"/>
</beans:bean>
<beans:bean id="mgrFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan" value="com.springmvc"/>
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</beans:property>
<beans:property name="jpaProperties">
<beans:props>
<beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</beans:prop>
<beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
<beans:prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</beans:prop>
<beans:prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</beans:prop>
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.format_sql">${hibernate.format_sql}</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
</beans:beans>
My controller is ,
#RestController
#RequestMapping("/mvchibernate")
public class CompanyController {
#Autowired
EmployeeService employeeService;
#GetMapping(value = "/getAllEmployees")
public List<Employee> getEmployeesList() {
#SuppressWarnings("unchecked")
List<Employee> listOfEmployees = employeeService.getAllEmployees();
return listOfEmployees;
}
}
And my service class like the following,
#Service
public class EmployeeService {
#Autowired
EmployeeDAO employeeDaoObj;
public List getAllEmployees() {
return employeeDaoObj.getAllEmployees();
}
}
And DAO implementation ,
#Repository
public class EmployeeDAO {
#PersistenceContext private EntityManager entityManager;
public List<Employee> getAllEmployees() {
String jpql = "SELECT e FROM Employee e";
TypedQuery<Employee> query = entityManager.createQuery(jpql, Employee.class);
return query.getResultList();
}
}
Here in my DAO class I autowired the entitymanager.And while running I am getting Cannot invoke javax.persistence.EntityManager and last showing that because "this.entityManager" is null.
So can anyone guide me to resolve this issue or kindly refer any documentation to follow please?
Edit from my previous answer, After reviewing your code below is what you are currently doing:
You created PersistenceConfig.java with Configuration annotation. This class is empty & it looks you have commented it after creating & defining beans.
Spring is actually checking PersistenceConfig.java for finding configurations instead of going to your xml.
Move your xml config to src/main/resources & for supporting best practice rename it to persistence.xml.
Now, you have to tell spring to read your configs from persistence.xml instead of looking anywhere else. Comment out Configuration annotation from PersistenceConfig.java & use below annotation in your main class :
:
#SpringBootApplication
#ImportResource({ "classpath:persistence.xml" })
public class SpringMvcHibernateRestApiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMvcHibernateRestApiApplication.class, args);
}
}
Also, I suggest to use org.springframework.orm.hibernate5.LocalSessionFactoryBean instead of
org.springframework.orm.hibernate4.LocalSessionFactoryBean as it will be inline with your latest spring & jpa versions.
Once you used this, you will be able to see entitymanager got created properly.
Edit : As per OP's comment, point number 5 is not required on their machine.
You are referencing the transactionManager bean here:
<tx:annotation-driven transaction-manager="transactionManager"/>
But the bean is actually named transactionMgr:
<beans:bean id="transactionMgr"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="MgrFactory"/>
</beans:bean>
You should reference the correct name:
<tx:annotation-driven transaction-manager="transactionMgr"/>
I do believe there are two options:
define PersistenceAnnotationBeanPostProcessor bean - it is responsible for injecting #PersistenceContext fields (though documentation says it should be registered automatically, m.b. it is worth to debug it to understand the reason):
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
define EntityManager factory bean and use #Autowired instead of #PersistenceContext:
<bean id="emf" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="mgrFactory"/>
</bean>

Hibernate, PostgreSQL : Write to 2 different databases on different servers.

I am working on a Spring-MVC application which we have running on 2 servers, one is our testing server, 2nd one is our live server. Is there any way that I can configure Hibernate to write any and all DB related queries on both servers. Example : User A saved Object A on live server, then write the same object on Test-server, vice-versa is not required. Our test and live server both have same setting and same database. Thank you.
root-context.xml :
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="org.postgresql.Driver"/>
<beans:property name="url"
value="jdbc:postgresql://localhost:PORT/DB_NAME"/>
<beans:property name="username" value="USERNAME"/>
<beans:property name="password" value="PASSWORD"/>
<beans:property name="removeAbandoned" value="true"/>
<beans:property name="removeAbandonedTimeout" value="20"/>
<beans:property name="defaultAutoCommit" value="false"/>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan" value="com.tooltank.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<!-- <beans:prop key="hibernate.jdbc.batch_size">1000</beans:prop>-->
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">true</beans:prop>
<beans:prop key="cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<hibernate.version>4.3.9.Final</hibernate.version>
If anymore data is required, please let me know. Thank you.
Not in any way that you're probably hoping for. They have completely different transactional contexts, so you can't just pretend that 2 databases are one.
What if one database throws an exception and the other one doesn't? Did the tx commit or roll back? Hibernate certainly doesn't expect it to be "half success".
You could make the test server a slave of the live server and just write to the live server. This is not a simple solution either and can be outright stupid.

dificullty while using two databases in the spring hibernate application

I am trying to use 2 databases in my spring project. It is working fine if I use 1 database but when add other databases I am getting following error message.
org.hibernate.HibernateException: No Session found for current thread
dispatcher-servlet
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="sample" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.kendoui.spring.models.Constraint</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.format_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<beans:bean id="Ascent" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<beans:property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=Ascent;" />
<beans:property name="username" value="sa" />
<beans:property name="password" value="abc123" />
<beans:property name="maxActive" value="1" />
<beans:property name="maxIdle" value="1" />
</beans:bean>
<beans:bean id="sessionFactory2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="Ascent" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.kendoui.spring.models.ItemOperationSequence</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.format_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager2" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory2" />
</beans:bean>
<!-- Enable annotation driven transactions. Required by getCurrentSession(). -->
<tx:annotation-driven/>
DAO implemantation
#Transactional("transactionManager2")
#Component
public class ItemOprSeqDaoImpl implements ItemOprSeqDao {
#Autowired
private SessionFactory sessionFactory;
#Override
public List<ItemOperationSequence> getList(String Itemcode) {
Session session = sessionFactory.getCurrentSession();
String queryStr= "select * from [#is_itemtrvoprdetails]";
Query query= session.createSQLQuery(queryStr).addEntity(ItemOperationSequence.class);
List <ItemOperationSequence> d=query.list();
return d;
}
}
I tried many different things but it is not working.
I tried using <beans:qualifier value="Ascent" /> in my servlet
and
#Qualifier(value="SessionFactory2") in my DAO
but nothing seems to work.
Anyone knows the answer to this problem. I would really appreciate if you could help.
Thanks in advance.
<tx:annotation-driven/> should be declared twice once for each transaction manager.
<tx:annotation-driven transaction-manager="transactionManager2"/>
and
<tx:annotation-driven transaction-manager="transactionManager"/>
Also add #Qualifier for
#Autowired
#Qualifier(value="SessionFactory2")
private SessionFactory sessionFactory;
You need to define a ChainedTransactionManager with both HibernateTransactionManagers as input.
I am not used to spring xml configuration, but it should look like this.
Than just use only #Transactional without a value.
<beans:bean id="transactionManager1" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<beans:bean id="transactionManager2" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory2" />
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.data.transaction.ChainedTransactionManager">
<beans:constructor-arg>
<list>
<beans:ref bean="transactionManager1" />
<beans:ref bean="transactionManager2" />
</list>
</beans:constructor-arg>
</beans:bean>
Okay I solved the problem by doing this.
first declared transaction-manager
<tx:annotation-driven transaction-manager="transactionManager1"/>
Then in my DAO used
#Transactional("transactionManager2")
#Component
public class ItemOprSeqDaoImpl implements ItemOprSeqDao {
#Autowired
#Qualifier("sessionFactory2")
private SessionFactory sessionFactory;
//then rest of the code
}
Its working fine now. Thanks you guys for your help.

How to insert into database when using JTA + JPA with Spring

I have a simple web application using JTA + JPA along with Spring (version 4).
I am doing a XA transaction using Atomikos JTA transaction Manager. This transaction involves inserting EmployeeA entity into DatabaseA and another EmployeeB entity into another DatabaseB.
Strangely, the application does not throw any error but no enteries are inserted into the Databases.
public class JtaTest {
public static void main( String[] args ) {
JtaTest test = new JtaTest();
test.testMandatory();
}
private static void testMandatory() {
final SpringContextUtil util = SpringContextUtil.instance();
ApplicationContext ctx = util.getApplicationContext();
final JtaEmployeeService employeeService = (JtaEmployeeService)ctx.getBean("jtaEmployeeService");
EmployeeA a = new EmployeeA();
a.setName("emp-a-1");
a.setAge(30);
EmployeeB b = new EmployeeB();
a.setName("emp-b-1");
a.setAge(31);
try {
employeeService.persistEmployees(a, b);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("success");
}
}
The service class implementation:
#Service("jtaEmployeeService")
public class JtaEmployeeServiceImpl implements JtaEmployeeService {
#Autowired
#Qualifier("employeea")
private GenericDao<Integer, EmployeeA> employeeADao;
#Autowired
#Qualifier("employeeb")
private GenericDao<Integer, EmployeeB> employeeBDao;
#Override
#Transactional( propagation=Propagation.REQUIRED, readOnly=false, isolation=Isolation.DEFAULT, rollbackFor=Exception.class)
public void persistEmployees(EmployeeA employeeA, EmployeeB employeeB) throws Exception {
employeeADao.save(employeeA);
employeeBDao.save(employeeB);
System.out.println("Saving employee A and employee B ");
}
}
The DAO implementation and interfaces:
#Repository("employeeb")
public class EmployeeBDaoImpl extends AbstractJTADaoDatabaseB implements
GenericDao<Integer, EmployeeB> {
#Override
public void save(EmployeeB entity) {
super.persist(entity);
}
}
#Repository("employeea")
public class EmployeeADaoImpl extends AbstractJTADaoDatabaseA implements
GenericDao<Integer, EmployeeA> {
#Override
public void save(EmployeeA entity) {
super.persist(entity);
}
}
public abstract class AbstractJTADaoDatabaseB {
#PersistenceContext(unitName = "persistenceUnitB")
#Qualifier("myManager")
private EntityManager entityManager;
public void persist(Object entity) {
entityManager.persist(entity);
// entityManager.flush();
}
protected EntityManager getEntityManager() {
return entityManager;
}
}
public abstract class AbstractJTADaoDatabaseA {
#PersistenceContext(unitName = "persistenceUnitA")
#Qualifier("myManager")
private EntityManager entityManager;
public void persist(Object entity) {
entityManager.persist(entity);
// entityManager.flush();
}
protected EntityManager getEntityManager() {
return entityManager;
}
}
Servlet-Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Enables annotaion driven transactional support on springs -->
<!-- <tx:annotation-driven transaction-manager="hTransactionManager"/> -->
<!-- <tx:jta-transaction-manager transaction-manager="jtaTransactionManager"/> -->
<tx:annotation-driven transaction-manager="jtaTransactionManager"/>
<!-- list the packages which are annotated with springs annotaions like #controller, #repository, #service, #component only -->
<context:component-scan base-package="com.goraksh.spring.tutorial.controller.business, com.goraksh.spring.tutorial.controller.rest, com.goraksh.spring.tutorial.service, com.goraksh.spring.tutorial.dao" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:application.properties"/>
</beans:bean>
<!-- <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"> -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</beans:prop>
<!-- <beans:prop key="hibernate.current_session_context_class">thread</beans:prop> -->
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.format_sql">${hibernate.format_sql}</beans:prop>
<beans:prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</beans:prop>
<beans:prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</beans:prop>
<beans:prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</beans:prop>
<beans:prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</beans:prop>
</beans:props>
</beans:property>
<!--<beans:property name="annotatedClasses"> -->
<!-- packagesToScan is meant to give the package where the javax.persistence.Entity classes are stored , recognised via their annotations-->
<beans:property name="packagesToScan">
<beans:list>
<beans:value>com.goraksh.spring.tutorial.model</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}"/>
<beans:property name="url" value="${jdbc.url}"/>
<beans:property name="username" value="${jdbc.username}"/>
<beans:property name="password" value="${jdbc.password}"/>
</beans:bean>
<beans:bean id = "hTransactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name = "sessionFactory" ref = "sessionFactory" />
</beans:bean>
<!-- Jata transactions with Atomikos -->
<beans:bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<beans:property name="uniqueResourceName"><beans:value>DataSourceA</beans:value></beans:property>
<beans:property name="xaDataSourceClassName"><beans:value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</beans:value></beans:property>
<beans:property name="xaProperties">
<beans:props>
<beans:prop key="databaseName">traningdb</beans:prop>
<beans:prop key="serverName">localhost</beans:prop>
<beans:prop key="port">3306</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password">root</beans:prop>
<beans:prop key="url">jdbc:mysql://localhost:3306/traningdb</beans:prop>
</beans:props>
</beans:property>
<beans:property name="minPoolSize"><beans:value>1</beans:value></beans:property>
</beans:bean>
<beans:bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<beans:property name="uniqueResourceName"><beans:value>DataSourceB</beans:value></beans:property>
<beans:property name="xaDataSourceClassName"><beans:value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</beans:value></beans:property>
<beans:property name="xaProperties">
<beans:props>
<beans:prop key="databaseName">traningdb2</beans:prop>
<beans:prop key="serverName">localhost</beans:prop>
<beans:prop key="port">3306</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password">root</beans:prop>
<beans:prop key="url">jdbc:mysql://localhost:3306/traningdb2</beans:prop>
</beans:props>
</beans:property>
<beans:property name="minPoolSize">
<beans:value>1</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="entityManagerFactoryA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="persistenceXmlLocation">
<beans:value>classpath:META-INF/persistence.xml</beans:value>
</beans:property>
<beans:property name="persistenceUnitName" value="persistenceUnitA" />
<beans:property name="dataSource" ref="dataSourceA" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="showSql" value="true" />
<beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="entityManagerFactoryB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="persistenceXmlLocation">
<beans:value>classpath:META-INF/persistence.xml</beans:value>
</beans:property>
<beans:property name="persistenceUnitName" value="persistenceUnitB" />
<beans:property name="dataSource" ref="dataSourceB" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="showSql" value="true" />
<beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<beans:property name="forceShutdown" value="false" />
<!-- <beans:property name="startupTransactionService" value="false" /> -->
</beans:bean>
<beans:bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<beans:property name="transactionTimeout" value="300" />
</beans:bean>
<beans:bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
depends-on="atomikosTransactionManager,atomikosUserTransaction">
<beans:qualifier value="myManager"/>
<beans:property name="transactionManager" ref="atomikosTransactionManager" />
<beans:property name="userTransaction" ref="atomikosUserTransaction" />
<beans:property name="allowCustomIsolationLevels" value="true" />
</beans:bean>
</beans:beans>
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnitA" >
<class>com.goraksh.spring.tutorial.model.EmployeeA</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnitB" >
<class>com.goraksh.spring.tutorial.model.EmployeeB</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</properties>
</persistence-unit>
</persistence>
Any help is highly solicited.
After lot of head-whacking and many hit-and-trial debugging, finally able to solve the problem.
Looks like below property is required in the persistence.xml
hibernate.transaction.jta.platform
On a application server there are multiple options available as Different JTA paltform services
But since I needed one standalone JTA platform implementation so I added one of my own.
com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform
So here is my modified persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnitA" transaction-type="JTA" >
<class>com.goraksh.spring.tutorial.model.EmployeeA</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.transaction.jta.platform" value="com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnitB" transaction-type="JTA">
<class>com.goraksh.spring.tutorial.model.EmployeeB</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.transaction.jta.platform" value="com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</properties>
</persistence-unit>
</persistence>
The source code for com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform
package com.goraksh.spring.atomikos.jta.platform;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
public final class AtomikosJtaPlatform extends AbstractJtaPlatform {
private TransactionManager utm = new UserTransactionManager();
private UserTransaction userTransaction = new UserTransactionImp();
/**
*
*/
private static final long serialVersionUID = -74991083213512919L;
#Override
protected TransactionManager locateTransactionManager() {
return utm;
}
#Override
protected UserTransaction locateUserTransaction() {
return userTransaction;
}
}
After some additional tuning:
1. Removing all hibernate ( jpa properties ) from persistence.xml to the servlet-context.xml.
Added jpaProperties to EntityManagerFactoryA and EntityManagerFactoryB
Servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Enables annotaion driven transactional support on springs -->
<!-- <tx:annotation-driven transaction-manager="hTransactionManager"/> -->
<!-- <tx:jta-transaction-manager transaction-manager="jtaTransactionManager"/> -->
<tx:annotation-driven transaction-manager="jtaTransactionManager"/>
<!-- list the packages which are annotated with springs annotaions like #controller, #repository, #service, #component only -->
<context:component-scan base-package="com.goraksh.spring.tutorial.controller.business, com.goraksh.spring.tutorial.controller.rest, com.goraksh.spring.tutorial.service, com.goraksh.spring.tutorial.dao" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:application.properties"/>
</beans:bean>
<!-- <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"> -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</beans:prop>
<!-- <beans:prop key="hibernate.current_session_context_class">thread</beans:prop> -->
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.format_sql">${hibernate.format_sql}</beans:prop>
<beans:prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</beans:prop>
<beans:prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</beans:prop>
<beans:prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</beans:prop>
<beans:prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</beans:prop>
</beans:props>
</beans:property>
<!--<beans:property name="annotatedClasses"> -->
<!-- packagesToScan is meant to give the package where the javax.persistence.Entity classes are stored , recognised via their annotations-->
<beans:property name="packagesToScan">
<beans:list>
<beans:value>com.goraksh.spring.tutorial.model</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}"/>
<beans:property name="url" value="${jdbc.url}"/>
<beans:property name="username" value="${jdbc.username}"/>
<beans:property name="password" value="${jdbc.password}"/>
</beans:bean>
<beans:bean id = "hTransactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name = "sessionFactory" ref = "sessionFactory" />
</beans:bean>
<!-- Jata transactions with Atomikos -->
<beans:bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<beans:property name="uniqueResourceName"><beans:value>DataSourceA</beans:value></beans:property>
<beans:property name="xaDataSourceClassName"><beans:value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</beans:value></beans:property>
<beans:property name="xaProperties">
<beans:props>
<beans:prop key="databaseName">traningdb</beans:prop>
<beans:prop key="serverName">localhost</beans:prop>
<beans:prop key="port">3306</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password">root</beans:prop>
<beans:prop key="url">jdbc:mysql://localhost:3306/traningdb</beans:prop>
</beans:props>
</beans:property>
<beans:property name="minPoolSize"><beans:value>1</beans:value></beans:property>
</beans:bean>
<beans:bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<beans:property name="uniqueResourceName"><beans:value>DataSourceB</beans:value></beans:property>
<beans:property name="xaDataSourceClassName"><beans:value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</beans:value></beans:property>
<beans:property name="xaProperties">
<beans:props>
<beans:prop key="databaseName">traningdb2</beans:prop>
<beans:prop key="serverName">localhost</beans:prop>
<beans:prop key="port">3306</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password">root</beans:prop>
<beans:prop key="url">jdbc:mysql://localhost:3306/traningdb2</beans:prop>
</beans:props>
</beans:property>
<beans:property name="minPoolSize">
<beans:value>1</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="entityManagerFactoryA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="persistenceXmlLocation">
<beans:value>classpath:META-INF/persistence.xml</beans:value>
</beans:property>
<beans:property name="persistenceUnitName" value="persistenceUnitA" />
<beans:property name="dataSource" ref="dataSourceA" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="showSql" value="true" />
<beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</beans:bean>
</beans:property>
<beans:property name="jpaProperties">
<beans:map>
<beans:entry key="hibernate.transaction.jta.platform" value-ref="atomikosJtaPlatform" />
<beans:entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</beans:map>
</beans:property>
</beans:bean>
<beans:bean id="entityManagerFactoryB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="persistenceXmlLocation">
<beans:value>classpath:META-INF/persistence.xml</beans:value>
</beans:property>
<beans:property name="persistenceUnitName" value="persistenceUnitB" />
<beans:property name="dataSource" ref="dataSourceB" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="showSql" value="true" />
<beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</beans:bean>
</beans:property>
<beans:property name="jpaProperties">
<beans:map>
<beans:entry key="hibernate.transaction.jta.platform" value-ref="atomikosJtaPlatform" />
<beans:entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</beans:map>
</beans:property>
</beans:bean>
<beans:bean id="atomikosJtaPlatform" class="com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform">
<beans:constructor-arg index="0" ref="atomikosTransactionManager"/>
<beans:constructor-arg index="1" ref="atomikosUserTransaction"/>
</beans:bean>
<beans:bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<beans:property name="forceShutdown" value="false" />
<!-- <beans:property name="startupTransactionService" value="false" /> -->
</beans:bean>
<beans:bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<beans:property name="transactionTimeout" value="300" />
</beans:bean>
<beans:bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
depends-on="atomikosTransactionManager,atomikosUserTransaction">
<beans:qualifier value="myManager"/>
<beans:property name="transactionManager" ref="atomikosTransactionManager" />
<beans:property name="userTransaction" ref="atomikosUserTransaction" />
<beans:property name="allowCustomIsolationLevels" value="true" />
</beans:bean>
</beans:beans>
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnitA" transaction-type="JTA" >
<class>com.goraksh.spring.tutorial.model.EmployeeA</class>
<exclude-unlisted-classes />
<properties>
<!-- <property name="hibernate.transaction.jta.platform" value="com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform" /> -->
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnitB" transaction-type="JTA">
<class>com.goraksh.spring.tutorial.model.EmployeeB</class>
<exclude-unlisted-classes />
<properties>
<!-- <property name="hibernate.transaction.jta.platform" value="com.goraksh.spring.atomikos.jta.platform.AtomikosJtaPlatform" /> -->
</properties>
</persistence-unit>
</persistence>

Spring MVC : Tying session attributes to each browser tab

I am working on Spring-MVC application in which I am setting and
getting some session attributes which I need in backend. The problem
is, Spring or the browser, someone out of the both is tying these
session attributes to different browsers rather than different tabs.
So if I open a new tab in the same browser, then it is updating the
session attribute for both tabs.
How can I remedy this problem? I tried using session to scope, request, etc. But nothing works. here is my controller and servlet-context.xml
Controller :
#Controller
#Scope("request")
public class PersonController {
#Secured("ROLE_USER")
#RequestMapping(value = "/loadsection/{id}")
public String loadNotePage(#PathVariable("id") Integer id, HttpSession session) {
// here i am setting the canvasid, which I would like to access in other methods
session.setAttribute("canvasid",id);
if (this.personService.returnCurrentOperationalMode()) {
session.setAttribute("canvasid",id);
return "redirect:/section/listing";
} else {
GroupCanvas mcanvas = this.groupCanvasService.getCanvasById(id);
this.personService.setCurrentCanvas(mcanvas.getMcanvasid());
return "redirect:/section/listing";
}
}
#Secured("ROLE_USER")
#RequestMapping(value = "/addbatchsections", method = RequestMethod.POST)
public #ResponseBody String addBatchSections(HttpSession session, #RequestBody Section[] sections) {
int canvasid = (Integer) session.getAttribute("canvasid");
try {
List<Section> sectionList = new ArrayList<>();
for (Section section : sections) {
sectionList.add(section);
}
this.sectionService.addBatchSections(sectionList,canvasid);
return "success";
} catch (Exception e) {
return "failure";
}
}
Servlet-Context.xml
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<resources mapping="/resources/" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="org.postgresql.Driver" />
<beans:property name="url"
value="jdbc:postgresql://localhost:5432/Person2"/>
<beans:property name="username" value="postgres" />
<beans:property name="password" value="asayhk2787" />
<beans:property name="removeAbandoned" value="true"/>
<beans:property name="removeAbandonedTimeout" value="20"/>
<beans:property name="defaultAutoCommit" value="false"/>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.journaldev.spring.model.Person</beans:value>
<beans:value>com.journaldev.spring.model.Notes</beans:value>
<beans:value>com.journaldev.spring.model.Canvas</beans:value>
<beans:value>com.journaldev.spring.model.Section</beans:value>
<beans:value>com.journaldev.spring.model.Attachment</beans:value>
<beans:value>com.journaldev.spring.model.GroupAccount</beans:value>
<beans:value>com.journaldev.spring.model.GroupMembers</beans:value>
<beans:value>com.journaldev.spring.model.GroupCanvas</beans:value>
<beans:value>com.journaldev.spring.model.GroupSection</beans:value>
<beans:value>com.journaldev.spring.model.GroupNotes</beans:value>
<beans:value>com.journaldev.spring.model.GroupAttachments</beans:value>
<beans:value>com.journaldev.spring.model.Token</beans:value>
<beans:value>com.journaldev.spring.model.WaitingMembers</beans:value>
<beans:value>com.journaldev.spring.model.NoteHistory</beans:value>
<beans:value>com.journaldev.spring.model.GroupNoteHistory</beans:value>
<beans:value>com.journaldev.spring.model.Feedback</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect
</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="connection.pool_size">200</beans:prop>
<beans:prop key="c3p0.max_size">200</beans:prop>
<beans:prop key="c3p0.timeout">1000</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">100</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</beans:bean>
I hope my question was clear, if there is any doubt, please feel free to ask me. Thanks a lot. :-)
Take a look at Spring Session and its associated examples.
Browsers like Chrome on new tab or new window share the user session because use the same session cookies.
Check this or change your session approach.

Categories