Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Yalvin Duha

Ranch Hand
+ Follow
since Apr 07, 2012
Yalvin likes ...
Eclipse IDE Slackware Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Yalvin Duha

Andrey Dmitriev wrote:    Try "com.model.NewsController"

   But without success...

The screenshot got me confused. I thought your controller was under src/main/java/com/model.

Create a new package under src/main/java/ and call it, "com.controller". Then move your NewsController in to that package.

Finally, change your newsController to:

   <bean id="newsController"
1 year ago

Al Hobbs wrote:So you catch the exception and then it doesn't come back as the right type of exception or what?   I don't think the Constraint violation is thrown inside the method which is why you aren't catching it.  Maybe I don't understand what your problem is.

The exceptions are thrown (runtime) when validations fail. I would like to know if there is a way I can catch them in some sort of custom handler/interceptor associated only to this method and throw a new exception back to the caller.

Those @Valid AOPs/advises are handled by some interceptor/post processor. I would like to know if there is some sort of abstraction they provide that I can tell it to ultimately throw my CustomException when validation fails, rather than ConstraintViolationException variation.

That way, my callee (myMethod) in the repository is always guaranteed to throw CustomException back to the caller, e.g. a service call, rather than in case of validation some sort of Bean Validation constraint exception.

1 year ago
Try "com.model.NewsController" as the class attribute of the bean rather than just "NewsController".
1 year ago
I'm using Spring Boot 2.0.1 with inclusion of validation-starter (which comes with Hibernate Validation 6.x) and trying to validate a method parameter and return objects in my Spring repository layer. There are basically a ParamObj and ReturnObj for this method,

ParamObj and ReturnObj both use a simple set of Bean Validation annotations on some of their properties, e.g @Pattern, @Min, etc.. My problem is that with these annotations, if either ParamObj or ReturnObj fails the validation process, MyRepoImpl#myMethod() would throw javax.validation.ConstraintViolationException.

However, as you can see, the contract for myMethod() mandates to throw a CustomException. Yes, ConstraintViolationException is a RuntimeException, however, now the caller of MyRepoImpl#myMethod(), e.g. a service layer logic, needs to catch this RuntimeException in addition to CustomException.

And the reason is, I need to prevent all the exceptions from bubbling out of the service layer (for this particular Spring @Service I have which calls this repository) -- this service is part of a long list of services that are called via a service orchestration which constructs a larger encompassing object. In other word, this piece (result coming obtained via MyRepoImpl) is considered optional, and subsequently, its failure should only be logged but do not stop the whole process.

For that reason, I can't utilize ControllerAdvice to capture ConstraintViolationException for the entire application.

My goal is to be able, when the validation fails for either method parameter or return, to throw only CustomException back to the caller.

Is this possible with annotations only? I looked into MethodValidationPostProcessor but can't figure out if I can implement my own to accomplish this goal.

P.S. The only way I can think to achieve this is to forgo @Valid/@Validated and call Validator#validate(obj, ...) programmatically for the parameter and return value inside the try-catch block, catch the constraint exception, and throw my CustomException(). However, I want to know if I can avoid that and leave this hard coded validation out of the repository.
1 year ago
I spent about 10 days preparing for the exam a few hours each day after work, mainly taking tests and reviewing some of bullet point notes I had from before. I took the exam today and passed with 90% score (80 questions).

The reason for my title is this, when I was taking the test, I counted about 35/80 questions that I was 70% unsure of their precise answers. The questions weren't necessarily crazy JLS tricky, but the focus was largely on fundamentals but with some twists that I had never tried or encountered. I knew going into the exam that I was no where near having a full grasp on certain topics, but didn't think I would go in there and encounter that many questions that I couldn't answer with 100% certainty.

I am more disappointed in myself because after taking the exam, I think I am light years behind of what I thought I should be (been developing with Java on and off for the past 2 years, but nothing hard core).

What I found out was that none of the resources I used, whether the famous/proverbial certification books or exam sites continuously being advertised here and else where, was useful. What I would do if I was going to start all over again is to just do a few tests but play around with the code myself to really get a hang of topics. Another thing that I realized is that the certification really doesn't make you a better programmer. It really doesn't, and if it wasn't for a voucher that my company provided, I wouldn't have taken it.

Just my 2 cent.
In realm of web services, I need to make the Schema types forward compatible for minor versioning. For instance, if we have two versions of a service deployed, i.e. WS_1.0 and WS_1.1, and two requesters, one developed against WS_1.0 (R_1.0) and the other against WS_1.1 (R_1.1), and all the R_1.0 requests are automatically routed to WS_1.1, this must not cause Schema validation failure:

One way to achieve this is to use a <any> wildcard, i.e.:


The reason <any>'s namespace is set to ##other is that XSD 1.0 validators, which apparently a lot of XML processors still use, fail the validation if the namespace was to set to ##targetNamespace or ##any due to Unique Particle Attribution (UPA) rule.

With that, this means for every minor versioning, I have to create a new XSD document with its own namespace, i.e.:


And the updated 1.0.xsd would look like:

Which makes the new instance look like:

As you can see, this does not scale well as every new minor, or major for that matter, versioning requires a new nested ComplexType inside of the previous parent element.

What other options do I have in order to get around this issue, or are there other ways to make the Schemas forward compatible without resorting to nesting elements for every version and creating a separate Schema document to define new ComplexType's in it? (Apparently XSD 1.1 allows ##targetNamespace or ##any but I cannot assume that all the consumers/requesters are going to develop against version 1.1, and most likely they would be using 1.0).

P.S. I don't even know whether the instance I posted is going to validate because <ExtraAddressInfo> is not in 1.1 namespace (only its child element). If that's the case, how does one get the extensions, i.e. <any> element, to be specified with a namespace other than ##other with XSD 1.0?

Joe Harry wrote:Any idea as to why the JDBC API is added as part of the exam?

Almost every project you work on has some back-end repository you need to communicate with. I think it's crucial for people to have a mediocre understanding of the basics on this topic.

I believe there should be a whole lot of other topics to be added to the exam, i.e. networking.
Yes. The point is (but don't take my words on it and someone else should correct me if I'm wrong), since there is no reference to the enclosing parent of the inner class that is being extended, you have to explicitly either pass it to the constructor and invoke its super() or create a new instance of that parent class and call its super() inside of the constructor to instantiate it.

Because, after all, the inner class you are extending is simply a member of that enclosing parent class, and you need a reference to it in order to construct the inner class. And the compiler is definitely creating a bunch of synthetic object references, i.e. this$0, this$1, etc., for each of the enclosing classes and providing these references to the inner class. So, I'm assuming for some reason it doesn't create such reference for the enclosing parent class, and the programmer should do it explicitly for the compiler.

Maybe someone else can accurately detail the process.
8 years ago

tanja valkin wrote: I found that there were many questions in the exam that werent in the Objectives.

Can you share the discrepencies of what was on the exam which wasn't in the objectives list?
Note: I typed this all up, but then realized the topic got locked (for good reason). I'm just going to post my comment even though Henry has given you an answer.

Yes, there is a way. The rules get moderately complicated.

What you need to do is to pass a reference of an innermost enclosing parent class of a class that you are extending, and call its super(). So, let's expand on your example,

A class that you are extending is Main.Inner.NestedInner and its innermost enclosing parent is Main.Inner, so we must pass its reference to Demo's constructor.

"Since an inner object can not exist without an instance of the enclosing class, the constructor for the inner class should have the enclosing class as one of the arguments; this is automatically provided by the compiler generated code in the class file. In addition, the first statement in the constructor should invoke the super class constructor with the following syntax: <enclosing instance>. super(). If the inner class that is extended has several layers of nesting, the enclosing instance immediately above the inner class is passed."

Now, if the constructor of a class that is being extended passes arguments, due to inheritance rules, you should call that particular constructor rather than the default one (super()). You are allowed to pass arguments to an overloaded constructor of a class that is extending an external inner class -- it's just that you need to, as I said, first, pass the parent of an innerclass extending class, then call its super().
8 years ago

Henry Wong wrote:If you haven't already guessed, synchronization is cooperative. The only way for one thread to prevent another thread from doing something, is for both threads to try to obtain the same lock -- the block is at the point of trying to obtain the lock (when the other thread owns it). A thread that doesn't synchronize can do whatever the thread wants to do.


Point taken. I was just under a wrong impression that once a lock is acquired, then any other thread would be prevented from accessing the object -- regardless of attempting to synchronize on it or not. That clears it.

The following code is confusing me a bit.

As you can see, we have two threads, "a" (Synch Accessor) and "b" (Unsynch Accessor), and two jobs, Runnable AccessingWithLock and AccessingWithoutLock, where both are accessing a shared object (SynchOrNoSynch.bObj which is an array of bytes).

Surprisingly, when the AccessingWithoutLock thread goes to sleep and AccessingWithLock enters the synchronized block, since AccessingWithLock has been engaged in intensive calculations, it will take some time for it to finish and you would not expect AccessingWithoutLock to be able to access bObj. To the contrary, it does get a hold of bObj , prints to the screen and its run() ends while the program waits for AccessingWithLock to finish its work, equally prints its outcome to the screen.

If one thread has a lock on an object, given that this thread would maintain the lock for certain duration, shouldn't it continue to have a hold on that lock until it releases it -- in this case exiting synchronized block in

Joey Sanchez wrote:

I don't understand why the first print line is 987.12346, what happened with the number 5?

Could someone explain this?

Thanks in advance!

I touched on this before but no one has given a definitive answer as to which methods we are supposed to watch for when it comes to rounding off.
As it has been said many times, the criteria and topics for the exam do alter; time compresion has been one of them.

According to the Oracle's website (OCPJP 6), the duration of the test is 150 mins.

Pi Xel wrote:B. Class BeSafe can create many instances of SafeDeposit. <---- I don't think so.....

So on the one hand you have the book saying that many instances can be created, and then on the other hand, saying that private int code cannot be changed (Which is the result of only being able to have one instance, not many)

You have to see the problem in the context of multi-threaded environment.

If you have multiple threads entering getInstance() method, and all lose their turn while between line "4" and "5", then once each of them become runnable, since they have by passed the conditional check (null), they will create new SafeDeposit instances. It's just that, since there won't be any reference to these instances, they will be lost and only the last one will remain.

So, in a sense, it does create many instances but under a very loosely definition which can be vague.

Finally, the option "F" states explicitly when the instance of SafeDeposit has been created, that is when the getInstance() method has returned, then the instance variable "code" cannot be changed which is true.