Giter VIP home page Giter VIP logo

Comments (13)

mnesarco avatar mnesarco commented on July 29, 2024 1

@iwangxiaodong @hazendaz What do you think about something like this:

  private static final String DEFAULT_JNDI_NAME = "java:comp/BeanManager";
  private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false);
  private static String jndiName = DEFAULT_JNDI_NAME; 

  private static void init() {
    if (!INITIALIZED.getAndSet(true)) {
      synchronized (DEFAULT_JNDI_NAME) {
        try {
          if (InitialContext.doLookup("java:comp/env/BeanManager") != null) {
            jndiName = "java:comp/env/BeanManager";
          }
        } catch (NamingException e) {
          // Fallback to default, do nothing
        }
      }
    }
  }

  private static BeanManager getBeanManager() {
    init();
    try {
      return InitialContext.doLookup(jndiName);
    } catch (NamingException e) {
      throw new RuntimeException(e);
    }
  }

from cdi.

hazendaz avatar hazendaz commented on July 29, 2024

Good catch! This would affect tomcat too as its jndi is read only as well. So bean manager is going to be in the location you noted above. I'd say drop back to 1.0.0 release for time being.

from cdi.

iwangxiaodong avatar iwangxiaodong commented on July 29, 2024

I copied CDIUtils.java and modified it as follows:

private static BeanManager getBeanManager() {
    try {
        return InitialContext.doLookup("java:comp/BeanManager");
    } catch (NamingException e) {
        try {
            return InitialContext.doLookup("java:comp/env/BeanManager");
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }
}

It works fine!

from cdi.

hazendaz avatar hazendaz commented on July 29, 2024

@iwangxiaodong Can you kindly send a PR for this. Thanks. I've used this exact same thing for JNDI many times in the past. While not the most efficient, given it hits multiple container types it's probably the easiest.

from cdi.

mnesarco avatar mnesarco commented on July 29, 2024

@iwangxiaodong @hazendaz It would be nice if we can cache at least the jndi name. but maybe it requires some synchronization. I think throwing exceptions and catching them every time this method is called is a lot of overhead. We have to decide what is less bad: synchronization vs try/catch. Or maybe we can statically do a lookup when the class is loaded and save the correct jndi name, so we will not require sync or try/catch inside the method.

from cdi.

mnesarco avatar mnesarco commented on July 29, 2024

@iwangxiaodong @hazendaz Or even better:

public final class CDIUtils {

  private static class JNDI {

    private static final String DEFAULT_JNDI_NAME = "java:comp/BeanManager";
    static final String NAME;

    static {
      String jndiName = DEFAULT_JNDI_NAME;
      try {
        if (InitialContext.doLookup("java:comp/env/BeanManager") != null) {
          jndiName = "java:comp/env/BeanManager";
        }
      } catch (NamingException e) {
        // Fallback to default, do nothing
      }
      NAME = jndiName;
    }

  }

  private static BeanManager getBeanManager() {
    try {
      return InitialContext.doLookup(JNDI.NAME);
    } catch (NamingException e) {
      throw new RuntimeException(e);
    }
  }


from cdi.

iwangxiaodong avatar iwangxiaodong commented on July 29, 2024

@mnesarco @hazendaz This code is very good performance!

from cdi.

mnesarco avatar mnesarco commented on July 29, 2024

@iwangxiaodong Thanks for the catch. I will submit the patch just now.

from cdi.

iwangxiaodong avatar iwangxiaodong commented on July 29, 2024

@mnesarco Yeah! Nice!

from cdi.

hazendaz avatar hazendaz commented on July 29, 2024

from cdi.

hazendaz avatar hazendaz commented on July 29, 2024

@iwangxiaodong @mnesarco I have released version 1.0.2 with this change. It should show in central in about 2 hours but is otherwise available for download now.

from cdi.

mnesarco avatar mnesarco commented on July 29, 2024

from cdi.

iwangxiaodong avatar iwangxiaodong commented on July 29, 2024

@hazendaz

compile 'org.mybatis:mybatis-cdi:1.0.2'

I tested OK!

from cdi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.