无法在静态Java方法中获取数据源

时间:2013-01-25 14:50:52

标签: java sql oracle jsf jsf-2

我正在尝试从CDI bean访问Oracle数据源。

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import javax.sql.DataSource;

@Named("ParentIDNameResolveController")
@ViewScoped
public class ParentIDNameResolve implements Serializable
{

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


    // Get the ID if the parent

    public static int ParentId(int chieldId) throws SQLException
    {

        int ParentId = 0;

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

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs;
        try
        {
            conn = ds.getConnection();
            ps = conn.prepareStatement("SELECT COMPONENTID, FKCOMPONENTID, COMPONENTSTATSID from COMPONENT where COMPONENTID = ?");
            ps.setLong(1, chieldId);
            rs = ps.executeQuery();

            while (rs.next())
            {
                ParentId = rs.getInt("FKCOMPONENTID");
            }

        }
        finally
        {
            if (ps != null)
            {
                ps.close();
            }
            if (conn != null)
            {
                conn.close();
            }
        }


        return ParentId;
    }

    // Get Parent Name

    public static String ParentName(int ParentId) throws SQLException
    {

        String ParentName = null;

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

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs;
        try
        {
            conn = ds.getConnection();
            ps = conn.prepareStatement("SELECT COMPONENTSTATSID, NAME from COMPONENTSTATS where COMPONENTSTATSID = ?");
            ps.setLong(1, ParentId);
            rs = ps.executeQuery();

            while (rs.next())
            {
                ParentName = rs.getString("NAME");
            }

        }
        finally
        {
            if (ps != null)
            {
                ps.close();
            }
            if (conn != null)
            {
                conn.close();
            }
        }


        return ParentName;
    }



}

不幸的是,当我从静态Java方法引用数据源时,我收到此错误:

Can't get data source

我不确定是否可以从静态Java方法访问数据源。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:2)

您不确定容器是否会向静态字段或静态方法注入任何带注释@Resource的内容。尝试重新考虑您的课程,并将其设为@ApplicationScoped,然后每个应用程序只有一个实例。

以下是您班级的一些变化:

@Named("ParentIDNameResolveController")
@javax.enterprise.context.ApplicationScoped  // not from javax.faces.bean
public class ParentIDNameResolve implements Serializable
{

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

  /* 
    Add getter/setter for DataSource
  */
  public DataSource getDataSource() {
    return this.ds;
  }

  public void DataSource setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  /* Change method signature to non static */
  public int ParentId(int chieldId) throws SQLException
  {

    DataSource ds = getDataSource();

    // your code here

    return ParentId;
  }

  /* Change method signature to non static */
  public String ParentName(int ParentId) throws SQLException
  {

    DataSource ds = getDataSource();

    // your code here

    return ParentName;
  }
}


接下来你可以在你的代码中使用它作为注入对象,你可以确定DataSource不会是null但是如果它是 - 检查是否正确定义为配置文件中的DataSource