This discussion is archived
8 Replies Latest reply: Jun 11, 2012 8:18 AM by gimbal2 RSS

JSF 2 what is the recommended way to use JDBC?

653831 Newbie
Currently Being Moderated
Hi,

I 'm using Glassfish v3.1, currently confused on how do i use Database access using JDBC in JSF 2. All my bean annotation is @ManagedBean, I have a class named as DBDao which primary responsibility is just to perform CRUD on db and shared across all my ManagedBean JSF page. How do i inject this class? do i use @SessionScoped? or @ApplicationScoped?

please advice
  • 1. Re: JSF 2 what is the recommended way to use JDBC?
    r035198x Pro
    Currently Being Moderated
    You would use @Inject (CDI annotation available in EE6).
    You'd use @SessionScoped or @ApplicationScoped to declare the scope of a bean not for injection.
    If all your managed beans need to do these crud operations then consider having a base crud bean where the DAO class is used and have the rest of your managed beans only deal with bean specific logic.
  • 2. Re: JSF 2 what is the recommended way to use JDBC?
    653831 Newbie
    Currently Being Moderated
    hi,

    thanks for quick reply, i don't get it,

    1) using @Inject which is CDI, how can that work with my @ManageBean ? the reason being i'm using @ManageBean is because my apps mainly use @ViewScoped where only usable in default JSF 2 implementation.

    2) how do i have a base crud bean with dao to deal with bean specific logic??

    please enlighten me more, thanks
  • 3. Re: JSF 2 what is the recommended way to use JDBC?
    gimbal2 Guru
    Currently Being Moderated
    When you're running on an application server I'd investigate using JPA in stead of low level JDBC. Better use the services provided to make your life easier!
  • 4. Re: JSF 2 what is the recommended way to use JDBC?
    653831 Newbie
    Currently Being Moderated
    hi,

    I knew JPA can make my life easier, but some of my actions were execute using Stored Procedure, returning complex type rather than varchar2 or number; but that's another matter, what i really confused and struggle now is that temporary now i'm injecting (using @ManagedProperty) Dao class into my @ManageBean. The Dao is itself annotated using @ApplicationScoped and set eager true. What suggested earlier was using @Inject, but that is totally CDI and i can't use it with current implementation.

    The reason i ask this is that with my current approach will this incur performance problem or worse, crash the app server?

    please advice,
  • 5. Re: JSF 2 what is the recommended way to use JDBC?
    gimbal2 Guru
    Currently Being Moderated
    Aha, the plot thickens. To invoke a stored procedure I tend to just lookup the datasource from JNDI and obtain a connection from it manually, of course using a try/finally block to ensure the connection is closed again upon finishing using it. It is possible to inject a datasource using the @Resource annotation; it depends on how configurable the stuff is if I use that or just do a lookup through InitialContext.
  • 6. Re: JSF 2 what is the recommended way to use JDBC?
    r035198x Pro
    Currently Being Moderated
    user650828 wrote:
    hi,

    The Dao is itself annotated using @ApplicationScoped and set eager true.
    Better make the DAO a stateless EJB to take advantage of container managed transactions and Inject a Datasource into it using @Resource.
    What suggested earlier was using @Inject, but that is totally CDI and i can't use it with current implementation.
    Why not? An EE6 container supports CDI. If you have it then prefer the CDI (unified api) annotations to the JSF ones for those scopes as well.
    >
    The reason i ask this is that with my current approach will this incur performance problem or worse, crash the app server?

    please advice,
    Well eager will make the bean be created at application server startup but it's used in beans that should not be created at application startup. Doesn't sound write to be initializing that bean there.
  • 7. Re: JSF 2 what is the recommended way to use JDBC?
    653831 Newbie
    Currently Being Moderated
    hi,

    does that mean i declared the dao as EJB @Stateless, inside I @Inject the datasource for DB operations, and in turn use @Inject again for the EJB to those @ManageBean that requires it? I thought once i declare certain say, JSF backing bean with @ManageBean, I can't use @Inject annotation? ... quite complicated..

    please enlighten me more..
  • 8. Re: JSF 2 what is the recommended way to use JDBC?
    gimbal2 Guru
    Currently Being Moderated
    It gets complicated because you'd be mixing the old way of doing it (@EJB, @Stateless, @Resource, etc.) with the CDI way of doing it (@Named, @Inject). I would stick to either the CDI way or the old way - so in this case use @Resource to inject the datasource.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points