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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:12:32+00:00 2026-06-15T11:12:32+00:00

Quick project explanation: We have a built application based on JSF2 + Spring with

  • 0

Quick project explanation: We have a built application based on JSF2 + Spring with Dynamic data sources. The data reference control is made with a spring-config:

<bean id="dataSource" class="com.xxxx.xxxx.CustomerRoutingDataSource">
....

and a class (referenced above):

public class CustomerRoutingDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
    return CustomerContextHolder.getCustomerType();
}

public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    return null;
}
}

the CustomerContextHolder called above is as follows:

public class CustomerContextHolder {

private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

public static void setCustomerType(String customerType) {
    contextHolder.set(customerType);
}

public static String getCustomerType() {

    String manager = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dataBaseManager");

    if (manager != null) {
        contextHolder.set(manager);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dataBaseManager", null);
    } else {
        String base =     (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentDatabBase");
        if (base != null)
            contextHolder.set(base);
    }
    return (String) contextHolder.get();
}

public static void clearCustomerType() {
    contextHolder.remove();
}
}

The problem is that the last guy is calling FacesContext.getCurrentInstance() to get the servlet context. Just to explain, it uses the session Attribute dataBaseManager to tell which base it should use.
For the actual solution it was working fine, but with the implementation of a RESTEASY web service, when we make a get request the FacesContext.getCurrentInstance() is obviously returning null and crashing.

I searched a lot and could not find a way of getting the servlet-context from outside of the @GET params. I would like to know if is there any way of getting it, or if there is another solution for my dynamic datasource problem.

Thanks!

  • 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-15T11:12:34+00:00Added an answer on June 15, 2026 at 11:12 am

    Like magic and probably not much people know.

    I searched deep into the Resteasy documentation, and found a part of springmvc plugin that comes with the resteasy jars, that has a class called RequestUtil.class.
    With that I was able to use the method getRequest() without the “@Context HttpServletRequest req” param.

    Using that I was able to set the desired database on the request attributes, and from another thread (called by spring) get it and load the stuff from the right place!

    I’m using it for a week now and it works like a charm. Only thing that I needed to do is change the determineLookupKey() above to this:

        @Override
    protected String determineCurrentLookupKey() {
        if (FacesContext.getCurrentInstance() == null) {
            //RESTEASY
            HttpServletRequest hsr = RequestUtil.getRequest();
            String lookUpKey = (String) hsr.getAttribute("dataBaseManager");
            return lookUpKey;
        }else{
            //JSF
            return CustomerContextHolder.getCustomerType();         
        }
    }
    

    Hope this helps other people!

    Thiago

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

Sidebar

Related Questions

Quick Explanation One Silverlight (3.0) project with several XAML pages. I want to load
I have a quick question....building a VS 2010 SharePoint project and I am referencing
How can I bookmark a project in TFS for quick access. we have a
Quick question about MSBuild. I have the following MSBuild file in directory d:\MyDirectory <Project
Quick question regarding Google Maps. I have one fully working Google Maps project and
Quick question so I start in the right direction. I have a multi project
I have a quick and simple question on a small project that I'm starting
Have a quick project I need to put together for windows that can have
I have picked up a quick PHP project that I need the use of
In a project I have to use HTML Quick forms to create elements. I

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.