• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EPractice Labs final exam question 18

 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A session bean uses several configurable constants. The constants are all defined as String types in JNDI. This cannot be changed because existing code is using the same JNDI information. One of the constant is a date, represented in string format. This date constant is used in multiple business methods of this session bean, actually as a Date object. Converting strings to dates is an expensive operation: therefore, the developer wants to do as little converting as possible.
Which two scenarios can be used to prevent converting from String to Date in every business method?
The model answers are :
c: Load the date string in an instance String variable by annotation of this instance variable and convert it to a Date type object in @PostConstruct annotated method.
d: Load the date string in an instance Date type variable by annotation of a setter method that takes a string and which carries out conversion and assigns the value to the instance variable.


But I think the answer is only c.
Reason why I think d is not correct:
if the bean is a stateless bean, its instance variable should not store any variable specific for a client. Consider this scenario:
1. a client first calls the setter method of the stateless bean instance which assigns a string value to an instance variable.
2. a client calls another business method of the bean and the instance is different from step 1 , the instance variable is not set in this new instance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himal,
I think both c and d are correct. In the questions, it says these dates are constants. Which means it isn't user specific data.

The example is silly anyway because converting between String and Date is not an expensive operation. But the principle still holds - how do you deal with constant data.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jeanne. Thanks for your reply.
Consider this:


For option c , if setDate business method is never called, the constant String object is never converted into Date object.

Or, if the bean is stateless , the date object is not a constant. So, the date object is specific to the bean instance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For d, I assumed the method gets called.

I agree it isn't a constant in the code. It's still logically a constant per the question description. And it has to be for this to work. Even if there are several instance variables representing the constant.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to demo this:

I assume the date constant is defined in the deployment descriptor and it is injected by the bean provider.

<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<display-name>echoejb </display-name>
<enterprise-beans>
<session>
<ejb-name>InjectionBean</ejb-name>
<ejb-class>com.ep.InjectionBean</ejb-class>
<session-type>Stateless</session-type>
<env-entry>
<env-entry-name>myString</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>2015-06-06</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
</ejb-jar>


 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For option d, the conversion can be done in the @PostConstruct method with the same deployment descriptor:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic