JDBC在Web应用程序中应该在哪里启动?

时间:2015-08-02 15:04:46

标签: jdbc web-applications jax-rs

我想在一开始就实现连接,并且我不想一次又一次地调用这个构造函数,因为它总是进入catch,抛出异常。

我已经在类数据库的构造函数中启动了JDBC,它看起来像这样,带有注释。

@Singleton
@Named
public class Database{

    public Database(){

        try {
            MysqlDataSource source; //and connection settings here

       javax.naming.Context ctx = new InitialContext();

            ctx.bind(Constants.CONTEXT_DATABASE_ID, source);
        } catch (NamingException ex) {
            Logger.getLogger(Web.class.getName()).log(Level.SEVERE, null, ex);
        }


    }


}

事实是,每次我在这个类上调用一个方法时,都会调用它的构造函数,因此,在try / catch方法中调用namecontext。如果要执行数据库的查询会更多,是否有办法避免每次都捕获此异常。

我认为这可能是解决方案之一。换句话说,有没有办法检查javax.naming.Context的命名上下文是否已包含JDBC的实例?

我该怎么办?

Content: true
Type: detectCustomCollations
Content: false
Type: allowPublicKeyRetrieval
Content: false
Type: dontCheckOnDuplicateKeyUpdateInSQL
Content: false
Type: readOnlyPropagatesToServer
Content: true
] at location [service jboss.naming.context.java.jdbc.global_Etiki_Users] [Root exception is org.jboss.msc.service.DuplicateServiceException: Service jboss.naming.context.java.jdbc.global_Etiki_Users is already registered]
    at org.jboss.as.naming.util.NamingUtils.namingException(NamingUtils.java:151)
    at org.jboss.as.naming.WritableServiceBasedNamingStore.bind(WritableServiceBasedNamingStore.java:104)
    at org.jboss.as.naming.WritableServiceBasedNamingStore.bind(WritableServiceBasedNamingStore.java:68)
    at org.jboss.as.naming.NamingContext.bind(NamingContext.java:253)
    at org.jboss.as.naming.InitialContext$DefaultInitialContext.bind(InitialContext.java:260)
    at org.jboss.as.naming.NamingContext.bind(NamingContext.java:262)
    at javax.naming.InitialContext.bind(InitialContext.java:425) [rt.jar:1.8.0_45]
    at javax.naming.InitialContext.bind(InitialContext.java:425) [rt.jar:1.8.0_45]
    at com.rockink.etiki.database.Database.<init>(Database.java:51) [classes:]
    at com.rockink.etiki.database.Database$Proxy$_$$_Weld$EnterpriseProxy$.<init>(Unknown Source) [classes:]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.8.0_45]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [rt.jar:1.8.0_45]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.8.0_45]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) [rt.jar:1.8.0_45]
    at java.lang.Class.newInstance(Class.java:442) [rt.jar:1.8.0_45]
    at org.jboss.weld.security.NewInstanceAction.run(NewInstanceAction.java:33) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_45]
    at org.jboss.weld.injection.producer.ejb.SessionBeanProxyInstantiator.newInstance(SessionBeanProxyInstantiator.java:56) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.bean.SessionBean.create(SessionBean.java:149) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:840) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:73) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:140) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
    at org.jboss.as.weld.injection.WeldInjectionContext.inject(WeldInjectionContext.java:39) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:51) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at

1 个答案:

答案 0 :(得分:0)

我得到了它。

经过测试,该类只调用一次@postconstruct,并且每次调用其方法时调用构造函数,并在调用的类中使用@Inject进行注释。

所以,将上面的代码放在@postconstruct中,你就不会再出错了。