Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

EPractice Labs final exam question 18

 
Ranch Foreman
Posts: 1897
12
  • 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.
 
author & internet detective
Posts: 39986
806
Eclipse IDE VI Editor Java
  • 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 Foreman
Posts: 1897
12
  • 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
Posts: 39986
806
Eclipse IDE VI Editor Java
  • 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 Foreman
Posts: 1897
12
  • 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 Foreman
Posts: 1897
12
  • 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:
 
He does not suffer fools gladly. But this tiny ad does:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic