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 8068637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:45:25+00:00 2026-06-05T12:45:25+00:00

I am working with JAXB 2.0 version. For this I am creating JAXBContext object

  • 0

I am working with JAXB 2.0 version. For this I am creating JAXBContext object in the following way:

package com;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class JAXBContextFactory {

    public static JAXBContext createJAXBContext() throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

        return jaxbContext;
    }

}

Basically since creating JAXBContext is very expensive, I want to create the JAXBContext once and only once for the entire application. So I put the JAXBContext code under static method as shown above.

Now the requests will call the JAXBContextFactory.createJAXBContext(); whenever it needs reference to JAXBContex. Now my question is , in this case is the JAXBContext created only once or will the application have multiple instances of JAXBContext?

  • 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-06-05T12:45:28+00:00Added an answer on June 5, 2026 at 12:45 pm

    Your application will have one instance of JAXBContext for each time this method is called.

    If you do not want this to happen, you need to do the following thing

    package com;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    
    public class JAXBContextFactory {
    
        private static JAXBContext context = null;
        public static synchronized JAXBContext createJAXBContext() throws JAXBException {
            if(context == null){
                context = JAXBContext.newInstance(Customer.class);
            }
            return context;
        }
    
    }
    

    The difference between this and your implementation is that in this one, we save the instance of JAXBContext that was created in a static variable (which is guaranteed to exist only once). In your implementation you are not saving the instance you just created anywhere, and will just create a new instance every time the method is called. Important: do not forget the synchronized keyword added to the method declaration, as it makes sure that calling this method in a multi-threaded environment will still work as expected.

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

Sidebar

Related Questions

Currently I'm working on converting object to XML, I notice all object properties are
I am working with Jaxb 2.x and was trying to validate XML document with
I'm working on to convert Hibernate objects to XML using JAXB. In our classes,
This is the first time I am working this intensly with XML in Java.
I'm working with a XML structure that looks like this: <ROOT> <ELEM_A> <A_DATE>20100825</A_DATE> <A_TIME>141500</A_TIME>
Working on group highlighting for a form based on this tutorial I found: http://www.jankoatwarpspeed.com/examples/ContextHighlighting/
I'm currently working on a Maven plugin that uses JAXB. The problem is that
I am working on generating Java objects from an XSD file using JAXB 2.1.
Working with H2 I get this error when I try to write a row
Working with JSF 2.0 (and Primefaces), is there a way to fire an ActionListener

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.