How to call datasource during bean initialization?

I have a managed bean that gets a list of settings and values ​​from a database table.

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/* include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;   

import org.glassfish.osgicdi.OSGiService;

@Named("DashboardController")
@SessionScoped
public class Dashboard implements Serializable {

    private String SessionTTL = null;
    private String MaxActiveUsers = null;
    private String SQL_Statement = null;
    private HashMap<String, String> settingsMap = null;

    public Dashboard() throws SQLException
    {
        initSettings();
        /*try { initSettings(); }
        catch (SQLException ex) { ex.printStackTrace(); }
         * 
         */
    }

    /* Call the Oracle JDBC Connection driver */
    @Resource(name = "jdbc/Oracle")
    private DataSource ds;



    public String settingValue(String key)
    {
        try
        {
            return (String) settingsMap.get(key);

        }
        catch(Exception x) { return "error - " + x.getMessage(); }

    }



    public void initSettings() throws SQLException
    {

        settingsMap = new HashMap<String, String>();

        if(ds == null) {
                throw new SQLException("Can't get data source");
        }


        Connection conn = ds.getConnection(); 

        if(conn == null) {
                throw new SQLException("Can't get database connection");
        }

        PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");

        try
        {
            //get data from database        
            ResultSet result = ps.executeQuery();
            while (result.next())
            {
               settingsMap.put(result.getString("SettingName"), result.getString("SettingValue"));
            }

        }
        finally
        {
            ps.close();
            conn.close();
        }


    }

}

      

When I run the code I get this error stack in Glassfish

  com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.DX_57.SM_57.Dashboard.
        at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
        at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
        at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
        at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
        at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
        at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
        at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
        at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
        at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
        at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
        at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
        at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
        at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
        at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
        at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
        at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
        at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
        at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
        at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
        at java.lang.Thread.run(Thread.java:722)
    Caused by: java.sql.SQLException: Can't get data source
        at com.DX_57.SM_57.Dashboard.initSettings(Dashboard.java:162)
        at com.DX_57.SM_57.Dashboard.<init>(Dashboard.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at java.lang.Class.newInstance0(Class.java:372)
        at java.lang.Class.newInstance(Class.java:325)
        at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
        ... 52 more

      

I am trying to initialize a hashmap in a bean constructor and then call the settingValue method in a jsf page passing a string parameter.

<h:panelGroup>Key</h:panelGroup>
<h:panelGroup>#{DashboardController.settingValue(SessionTTL)}
</h:panelGroup>

      

Is it possible to initialize the datasource before calling the constructor bean. How can I execute SQL queries right after bean initialization.

+1


source to share


1 answer


Is it possible to initialize the datasource before calling the constructor bean. How can I execute SQL queries right after bean initialization.

It's impossible. Dependency resources can only be injected after the instance has been built. Without a concrete example, there is simply nothing where resources can be injected.

An annotation was invented to take action immediately after dependency injection @PostConstruct

. You need to use it instead of the constructor.

Replace

public Dashboard() throws SQLException {
    initSettings();
}

public void initSettings() throws SQLException {
    // ...
}

      

by

@PostConstruct
public void initSettings() throws SQLException {
    // ...
}

      

Method annotated using @PostConstruct

, called immediately after the dependency injection, as it was configured @Resource

, @Inject

, @EJB

, @ManagedProperty

etc.




Unrelated to the specific issue, the way you access the value of the card with an additional method is unnecessarily complicated. Just have a simple getter for it

public Map<String, String> getSettings() {
    return settingsMap;
}

      

and access the map values ​​like this:

#{DashboardController.settings['SessionTTL']}

      

or when the card key does not contain periods:

#{DashboardController.settings.SessionTTL}

      

See also:

By the way, initial uppercase instance names such as #{DashboardController}

, and not lowercase for example #{DashboardController}

(or just the default #{dashboard}

) are also bad coding conventions.

+4


source







All Articles