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

  • Home
  • SEARCH
  • 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 6146351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:56:05+00:00 2026-05-23T18:56:05+00:00

I have to code an Stateless EJB3 that uses callable statements, and builds a

  • 0

I have to code an Stateless EJB3 that uses callable statements, and builds a XML structure.
I’m getting a DataSource Connection from JNDI;

ctx.lookup('DS').getConnection();

my ejb has lots of methods(that build each part of the XML), and i found it is cumbersome to pass the connection as a parameter to every method that i could call.

the thing is:

Should I put the connection as a field in my ejb, open it on @PostConstruct and close it only on @PreRemove? so that it would use the same connection OPEN for the entire lifetime of the bean.

or

Should I use an Interceptor with a try/cacth block that opens the connection and asign it to the bean on every call? Moreover wouldn’t this cause concurrency problems? given the connection is a field and is shared by the threads calling the stateless bean methods?

I think I want the same thing we can do with JPA:

@PersistenceContext EntityManager em;

public String businessMethod(String param){
    em.find(...);
    someMethod(param);
}

private void sometMethod(String param){
    em.createQuery...;
}

but with java.sql.Connection

thanks in advance

Victor

  • 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-23T18:56:06+00:00Added an answer on May 23, 2026 at 6:56 pm

    To avoid passing connection as parameter to all methods, you can try field-based injection for the data-source.

    @Resource(name="defaultDS")
        private javax.sql.DataSource defaultDS;
    

    Then you can have the data-source available in all the methods present in that bean & can get connection from it.

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

Sidebar

Related Questions

I have inherited a project that uses the following pattern for passing parameters from
I have code which uses the StreamReader to read HTML from a file, then
I have code that pics a random number from 0 to 1. I am
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have been reading that creating dependencies by using static classes/singletons in code, is
I have code written that converts my Document to a string prior to printing
I have code that interacts with a gridview, and the code is exactly the
I would like to use http://code.google.com/p/stateless in my code to separate the functionality from
I have the following code: @Name(myService) @Scope(ScopeType.APPLICATION) @Stateless public class MyService { private Service

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.