Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:09:00+00:00 2026-05-14T22:09:00+00:00

Scenario: I have @Singleton UserFactory ( @Stateless could be) , its method createSession() generating

  • 0

Scenario:

I have @Singleton UserFactory (@Stateless could be) , its method createSession() generating @Stateful UserSession bean by manual lookup.

If I am injecting by DI @EJB – i will get same instance during calling fromFactory() method(as it should be)

What I want – is to get new instance of UserSession without preforming lookup.

Q1: how could I call new instance of @Stateful session bean?

Code:

@Singleton
@Startup
@LocalBean
public class UserFactory {

    @EJB
    private UserSession session;

    public UserFactory() {
    }


    @Schedule(second =  "*/1", minute = "*", hour = "*")
    public void creatingInstances(){
        try {
            InitialContext ctx = new InitialContext();
            UserSession session2 = (UserSession) ctx.lookup("java:global/inferno/lic/UserSession");
            System.out.println("in singleton UUID " +session2.getSessionUUID());    
        } catch (NamingException e) {
            e.printStackTrace();
        }

    }

    @Schedule(second =  "*/1", minute = "*", hour = "*")
    public void fromFactory(){
        System.out.println("in singleton UUID " +session.getSessionUUID());
    }


    public UserSession creatSession(){
        UserSession session2 = null;
        try {
            InitialContext ctx = new InitialContext();
            session2 = (UserSession) ctx.lookup("java:global/inferno/lic/UserSession");
            System.out.println("in singleton UUID " +session2.getSessionUUID());
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return session2;

    }



}

As I understand, calling of

session.getClass().newInstance();

is not a best idea

Q2 : is it true?


#

Update

Goals

In reality the goal is to create some SessionsFactory that that would managed user`s sessions (this is web services users)

The Session @Statefull bean :

@Stateful
//Destroying outomaticly after 30 minuts inactive
@StatefulTimeout(180000)
@LocalBean
public class UserSession {
    //represent creation time, used by scheduler for destroying session
    private GregorianCalendar creationDate;
    private UUID sessionUUID;
    private String userId;
    private String state;

    //TODO change to xml
    private String histrory;

    public UserSession() {
    }
    @PostConstruct
    private void initSession(){
        //getting construction date
        creationDate = (GregorianCalendar) Calendar.getInstance();
        //generationg session id
        sessionUUID = UUID.randomUUID();
    }

    @PreDestroy
    private void releaseResource(){
        creationDate =null;
        sessionUUID = null;
        userId =null;
    }

    @Remove
    public void destroySession(){

    }

    public UUID getSessionUUID() {
        return sessionUUID;
    }

    public GregorianCalendar getCreationDate() {
        return creationDate;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getHistrory() {
        return histrory;
    }

    public void addHistroryEntry(String entry) {
        //TODO add history entry

    }
}

In factory methods I want just create new instance of @Statefull UserSession and to manage number of created sessions for each user, and call destroySession() after some period (30 minutes)

I need to track the history of user`s sessions requests , and persists there history later..

So I think @Statefull bean should suet my needs. But it looks like the lookup by JNDI name is the only chance to be shore that new ejb will be created. I am searching for possibility
to inject new instance of ejb without lookups, and maybe possibility to get collection of currently created instances of my @Statefull UserSession instead of keeping thrm in some map/collection.

Q3: so.. only JNDI will help me to create new instance of ejb ?

Q4: Is it possible to get collection of some ejb`s instances from container?


I am using glassfish v3, ejb 3.1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:09:01+00:00Added an answer on May 14, 2026 at 10:09 pm

    Q1: how could I call new instance of @Stateful session bean?

    You must not inject a Stateful Session Bean into a stateless object such as Stateless
    Session Bean or Servlet that may be shared by multiple concurrent clients, you should use JNDI instead. Period.

    To be honest, I’m not sure to understand what you are doing and I don’t see important steps such as removal of your Stateful Session Beans. You are likely going to run out of memory or cause a lot of disk IO as the container that will try to passivate/activate instances to save memory.

    Q2 : is it true?

    You can call new but don’t expect to get something else than a simple Java class i.e. don’t expect to get a managed object i.e. don’t expect to get an EJB. I don’t think that this is what you want.


    Sorry if this doesn’t help much but as I said, I don’t really understand what you’re trying to achieve. You should maybe start to explain your goal first, I don’t have the feeling that you are on the right path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd consider using logging.handlers.SocketHandler for the message passing parts of… May 14, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer You must not restore file with cat. The proper way… May 14, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Insert INTO tblTest (fieldOne,FieldTwo) VALUES ('valueOne','valueTwo'). May 14, 2026 at 10:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.