@Test public void removesTheCreatedSessionFromInvocationContextAndKeepsTheOtherOne() throws Exception { contextData.put("hibersap.session.jndiName1", mock(Session.class)); when(initialCtx.lookup("jndiName2")).thenReturn(sessionManager); interceptor.injectSessionsIntoEjb(invocationContext); assertThat(contextData).hasSize(1); }
@AroundInvoke public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception { Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget()); Map<Session, String> sessionsCreated = new HashMap<>(); try { for (Field sessionField : sessionFields) { String jndiName = getSessionManagerJndiName(sessionField); String key = HIBERSAP_SESSION_PREFIX + jndiName; Session session = (Session) ctx.getContextData().get(key); if (session == null) { LOGGER.debug("Create Hibersap session for SessionManager " + jndiName); session = openSession(jndiName); sessionsCreated.put(session, jndiName); ctx.getContextData().put(key, session); } injectSessionIntoTarget(ctx.getTarget(), sessionField, session); } return ctx.proceed(); } finally { closeSessions(sessionsCreated, ctx.getContextData()); } }
HibersapSessionInterceptor { @AroundInvoke public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception { Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget()); Map<Session, String> sessionsCreated = new HashMap<>(); try { for (Field sessionField : sessionFields) { String jndiName = getSessionManagerJndiName(sessionField); String key = HIBERSAP_SESSION_PREFIX + jndiName; Session session = (Session) ctx.getContextData().get(key); if (session == null) { LOGGER.debug("Create Hibersap session for SessionManager " + jndiName); session = openSession(jndiName); sessionsCreated.put(session, jndiName); ctx.getContextData().put(key, session); } injectSessionIntoTarget(ctx.getTarget(), sessionField, session); } return ctx.proceed(); } finally { closeSessions(sessionsCreated, ctx.getContextData()); } } }
HibersapSessionInterceptor { @AroundInvoke public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception { Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget()); Map<Session, String> sessionsCreated = new HashMap<>(); try { for (Field sessionField : sessionFields) { String jndiName = getSessionManagerJndiName(sessionField); String key = HIBERSAP_SESSION_PREFIX + jndiName; Session session = (Session) ctx.getContextData().get(key); if (session == null) { LOGGER.debug("Create Hibersap session for SessionManager " + jndiName); session = openSession(jndiName); sessionsCreated.put(session, jndiName); ctx.getContextData().put(key, session); } injectSessionIntoTarget(ctx.getTarget(), sessionField, session); } return ctx.proceed(); } finally { closeSessions(sessionsCreated, ctx.getContextData()); } }  }
HibersapSessionInterceptor { @AroundInvoke public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception { Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget()); Map<Session, String> sessionsCreated = new HashMap<>(); try { for (Field sessionField : sessionFields) { String jndiName = getSessionManagerJndiName(sessionField); String key = HIBERSAP_SESSION_PREFIX + jndiName; Session session = (Session) ctx.getContextData().get(key); if (session == null) { LOGGER.debug("Create Hibersap session for SessionManager " + jndiName); session = openSession(jndiName); sessionsCreated.put(session, jndiName); ctx.getContextData().put(key, session); } injectSessionIntoTarget(ctx.getTarget(), sessionField, session); } return ctx.proceed(); } finally { closeSessions(sessionsCreated, ctx.getContextData()); } }  @AroundInvoke Object injectSessionsIntoEjb(final InvocationContext ctx); }
HibersapSessionInterceptor { @AroundInvoke public Object injectSessionsIntoEjb(final InvocationContext ctx) throws Exception { Set<Field> sessionFields = getHibersapSessionFields(ctx.getTarget()); Map<Session, String> sessionsCreated = new HashMap<>(); try { for (Field sessionField : sessionFields) { String jndiName = getSessionManagerJndiName(sessionField); String key = HIBERSAP_SESSION_PREFIX + jndiName; Session session = (Session) ctx.getContextData().get(key); if (session == null) { LOGGER.debug("Create Hibersap session for SessionManager " + jndiName); session = openSession(jndiName); sessionsCreated.put(session, jndiName); ctx.getContextData().put(key, session); } injectSessionIntoTarget(ctx.getTarget(), sessionField, session); } return ctx.proceed(); } finally { closeSessions(sessionsCreated, ctx.getContextData()); } }  @AroundInvoke Object injectSessionsIntoEjb(final InvocationContext ctx);  }