如何获得与jboss的数据源连接为7 resteasy

时间:2020-09-02 18:23:29

标签: java database rest jboss java-ee-6

通常,在使用Servlet时,我通常在ServletContextListener中初始化数据源,如下所示:

public class DBConnectionListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {     
            // Obtain our environment naming context
            Context ctx = new InitialContext();

            // Look up data source web service in order to determine whether ws is up or not
            DataSource dsWs = (DataSource) ctx.lookup("java:jboss/datasources/wsmanager");
            DataSource dsnew = (DataSource) ctx.lookup("java:jboss/datasources/mraDBNew");
            sce.getServletContext().setAttribute("DBCPool", dsnew);
            sce.getServletContext().setAttribute("wsmngr", dsWs);

        } catch (NamingException e) {
          e.printStackTrace();
            Utility.LogError(e);
        }
    }

}

然后在每个servlet中,我在初始化生命周期中获得连接,如下所示:

     public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        dataSourceDBC = (DataSource) getServletContext().getAttribute("DBCPool");
        dataSourceWSMNGR = (DataSource) getServletContext().getAttribute("wsmngr");
      }

  public static synchronized Connection getProfileConnection() throws SQLException {
    return dataSourceDBC.getConnection();
  }

我只需要一些帮助,以按照下面定义的端点在jboss中使用Resteasy实现相同的目的

@Path("/session")
public class ReservationSessionEndpoint {
    /**
     * Method that will return the newly created session
     * 
     * @param reservationTicketId
     * @return
     */
    @POST
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public Response createSession(@HeaderParam("reservationTicketId") String reservationTicketId) {
        return Response.status(200).build();
    }
}

感谢您的帮助, 阿什利

0 个答案:

没有答案