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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »


Blacky1

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

Post 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.

cjscript
Posts: 8
Joined: Tue Jan 29, 2013 4:14 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests