hibernate - Allow EntityManagerFactory injection to be null when database service is off -


i have jersey web service uses hibernate persistence. have implemented creation/disposal of entitymanagerfactory using hk2 provider logic found stack overflow, has helped keep number of db connections low. don't want force users have db, want code gracefully handle case. however, can't seem figure out, other having comment out @inject annotation. know how use custom @inject , code allow null?

i tried catch exception in dbmanager when persistence.create fails , check null in webserviceclass. crashes @ @inject line , exception not caught. looked around @ findorcreate null exception , saw there method called supportsnullcreation() have not found examples on how use it.

this code looks like:

injectable db provider using hk2:

public class dbmanager          implements factory<entitymanagerfactory> {    private static entitymanagerfactory factory = null;     @inject    public dbmanager()    {       try       {          factory = persistence.createentitymanagerfactory("myapp");       }       catch ( exception eee )       {          // means db not connected want allow          system.out.println("no db, should okay");       }    }     @override    public entitymanagerfactory provide() {       return factory;    }     @override    public void dispose(entitymanagerfactory emf) {       if ( emf != null && emf.isopen() )       {          emf.close();       }    }     public entitymanagerfactory getentitymgrfactory()    {       return factory;    } } 

then here how provider created in jersey app:

@applicationpath("rest") public class myapplication extends resourceconfig  {     public myapplication()     {          ....      // provider of db     this.register( new abstractbinder()     {        @override        public void configure()        {  bindfactory(dbmanager.class).to(entitymanagerfactory.class).in(singleton.class);        }     }); } 

then used this:

@singleton @path("myservice") public class webserviceclass {     // note: right have comment run without db    @inject    private entitymanagerfactory entitymanagerfactory = null;    ... 

the exception this...

java.lang.illegalstateexception: context   org.jvnet.hk2.internal.singletoncontext@6cae5847 findorcreate returned null  descriptor systemdescriptor(     implementation=com.db.dbmanager     contracts={javax.persistence.entitymanagerfactory}     scope=javax.inject.singleton     qualifiers={}     descriptortype=provide_method     descriptorvisibility=normal     metadata=     rank=0     loader=org.glassfish.hk2.utilities.binding.abstractbinder$2@7050f2b1     proxiable=null     proxyforsamescope=null     analysisname=null     id=145     locatorid=0     identityhashcode=863132354     reified=true)     @ org.jvnet.hk2.internal.utilities.createservice(utilities.java:2075) ... 

what want dynamic injection of emf dependency. injections static, means dependencies wired when object depends on them created.

an easy solution dependency injection mechanism, not hk2, create wrapper object, hold reference real dependency, or null if dependency not available. same may achieved wrapping entitymanagerfactory collection of 1 or 0 elements. this:

// factory wrapper public class emfholder {    private entitymanagerfactory emf;    public emfholder(entitymanagerfactory emf) { this.emf = emf;}    public entitymanagerfactory getemf() { return this.emf;  }  // provider public class dbmanager implements factory<entitymanagerfactory> {  // ...     @override    public emfholder provide() {       return new emfholder(factory);    }  // ... }  // using factory if not null public class webserviceclass {    @inject    private emfholder emfholder;     public void docomethingwithemf() {      if (emfholder.getemf() != null) {         // factory...      }    } } 

also have @ hk2 iterable provider, don't think allow return null value provide method.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -