0

In my JSF application @PostConstruct mentioned in initApplication() method and @PreDestroy is used to close all database connections

I gave more than 2 users to login same time. When any one Logged out session is invalidated by @PreDestroy. So other users also can not get connection.

I want to call @PreDestroy only when my Application scope terminated, not for session invalidation.

How can i do it?

Manoj
  • 311
  • 2
  • 5
  • 14
  • Wrong approach. Use a connection pool. Related: http://stackoverflow.com/questions/9428573/is-it-safe-to-use-a-static-java-sql-connection-instance-in-a-multithreaded-syste/9431863#9431863 Or if your container supports it, better JPA+EJB. – BalusC Jun 30 '13 at 10:55
  • I'm using JPA EntityManager for connection. So i should not close EntityManager in PreDestory. Right? – Manoj Jul 01 '13 at 05:19
  • So you're manually creating and managing `EntityManagerFactory`, `EntityManager` and `UserTransaction`? If your container supported EJB (and JTA), you'd not have the need to do this all by just having it injected through `@PersistenceContext` in EJB and performing the job whith needs to go in a single transaction in a single `@Stateless` EJB method. If your container didn't support it, you'd need to rewrite the code as such that `EntityManagerFactory` is application scoped and that `EntityManager` is created and destroyed at the same moments as the transaction itself. – BalusC Jul 01 '13 at 12:18
  • hi thanks for ur valuable comment. i have used emf is application scope only. this web application used by our suppor team only once in a week maximum. so i thought i don't want emf in open state always. when one user logged in its workin emf opened n closed. when 2 users logged in after anyone logged out emf closed. another user can't get emf. – Manoj Jul 01 '13 at 17:39

0 Answers0