Page 1 of 1

About Question com.enthuware.ets.scbcd.v5.2.18

Posted: Fri Dec 03, 2010 9:47 am
by ETS User

Re: About Question com.enthuware.ets.scbcd.v5.2.18

Posted: Fri Dec 03, 2010 1:46 pm
by Blacky1
Hello,

I don't understand the aims of the Class/Type level resource annotation.

Why to declare a Class/Type level resource annotation :

Code: Select all

  @Resource(name="jdbc/mydatabase", type=javax.sql.DataSource.class, mappedName="java:/DefaultDS" )  
   @Stateless  
   public class GeneralEJB3Knowledge implements GeneralEJB3KnowledgeRemote {  
          
          
        @Resource(name="jdbc/mydatabase") javax.sql.DataSource datasource;  
          
          
        public String testResourceRef() {  
            
           String result = "Connection has been retrieved !!";  
           try {  
               datasource.getConnection().close();  
                 
           } catch (Exception e) {  
                 
               e.printStackTrace();  
               result = "Exception caught: " + e.getClass().getName();  
           }  
             
           return result;  
       }  
         
     
   }  
When it is working without :

Code: Select all

 @Stateless  
    public class GeneralEJB3Knowledge implements GeneralEJB3KnowledgeRemote {  
          
          
        @Resource(name="jdbc/mydatabase", mappedName="java:/DefaultDS" ) javax.sql.DataSource datasource;  
          
          
        public String testResourceRef() {  
              
           String result = "Connection has been retrieved !!";  
           try {  
               datasource.getConnection().close();  
                 
           } catch (Exception e) {  
                 
               e.printStackTrace();  
               result = "Exception caught: " + e.getClass().getName();  
           }  
             
           return result;  
       }  
         
     
   }  
Could someone explain to me the difference and the goal of the class level resource annotation ?
Thank you by advance for your help.

Re: About Question com.enthuware.ets.scbcd.v5.2.18

Posted: Sat Aug 10, 2013 11:58 am
by cjscript
Late reply but might help someone.

The Class/Type level annotation makes the resource available in the bean. Once available, you can use Jndi to get the resource (InitialContext and EJBContext). You wouldn't use Property annotation also. If Property annotation is used, you don't need Type annotation.