Hibernate Exception Fixed By Alt+Tab - java

I've got a very curious problem in Hibernate that I would like some opinions on. In my code if I do the following:
Go to page A
Click a link on page A to be taken to page B
Click on data item on page B
Exception thrown
I get an error telling me:
failed to lazily initialize a collection of role: XYZ, no session or session was closed
Fair enough. But when I do the same thing but add an alt+tab in the middle, everything is fine. E.g.
Go to page A
Click a link on page A to be taken to page B
Hit ALt+Tab to switch to another application
Hit ALt+Tab to switch back to the web browser
Click on data item on page B
Everything is fine.
I'm a little confused as to how switching focus from my application makes it act as I want it to. Does anyone have any light to shine on the subject? I don't think it's a locking issue as even if I do the second set of steps quicker than the first, still no error.
It's a Seam application using Hibernate 3.3.2.GA & 3.4.0.GA.

It's not really related to switching the applications; maybe your browser updates something during focus lost/gained.
The reason for your issue is this:
In the first request, you load an object and store that in the session. This object contains a collection of some kind.
In the next request, you try to do something with the collection but you forgot to call session.refresh(object), first.
What happens is that the session is automatically closed when the first request is finished. But the backed collection in your object still contains a reference to it (so that it can be loaded automatically when you access it for the first time).

are you using javascript? Maybe an event is firing when you switch back to the browser that somehow clears the bad state?

Does your page complete it's rendering? I have found that sometimes when I get errors, instead of displaying the error, the server simply quits producing output. It outputs the error to the log, but thats about it. I end up with a half completed page. View source and see if you get to the end of the body, perhaps your switching caused the server to get lazy, or your browser to terminate the connection prematurely when the error occured

My advice is to do a unit test to see if you can reproduce the error in other environment isolated from the browser ;)

Related

The user is anonymous after returning from a redirect

we implemented a paymentmethod with saved creditcards and 3dsecure (SAP commerce/hybris), but it's not always working. Sometimes a nullpointer for the orderData is thrown, after returning from the paymentservice. The reason for this seems to be, that the user sometimes is anonynoums after the return, so the orderData isn't visible.(the orderData exists by the way)
But I have no idea why this happens. And like I said it only happens occasionally on prod, but when I debbuged local it happend most of the time. And at the paymenservice the payment is authorized.
But it's difficult to debug, because there aren't any testcards to test with 3dscure, so I have to use a real one everytime.
To be honest, I am not sure if I am allowed to show the code here, so please forgive me, for not showing it now (I didn't write it).
Are there any "common" mistakes/suggestions/best-practices or whatever? Or maybe just an idea?
Oh, and we don't face this problem with other returns like paypal.
It's a bit tricky, since I don't know the code, but here are some advices:
Maybe the Session Cookie in the browser is destroyed somehow? You can check with the Browsers developer tools if the cookie is contained in all requests (Network Tab -> Click on the Request -> Request Header). This can sometimes happen if you switch between http/https or multiple subdomains.
Bad timing with an automatic logout of the user?
Are you using multiple cluster nodes? Maybe the session is not yet synchronised between the nodes?

Apache Wicket: Display an error message when StalePageException occurs

I have a Java application using the Apache Wicket 6.x; sometimes exception StalePageException is triggered as described below and shows up in the log. However, because there is no feedback to the user, the application behaves unexpectedly causing frustration.
How can I capture the StalePageException and present a message to the user (preferably using a feedback panel) explaining the situation?
Open a stateful page in a tab/window, then copy the url from the
address bar and open it in a new tab/window. Then go back to the first
tab/window and try to click on any stateful link. This will lead to
StalePageException.
It is thrown because Wicket detects that the same page instance has
been rendered between the render of the current page and the click on
the link. Wicket does this because it is not sure whether there are
any changes in the page tree hierarchy between the initial rendering
and the click event. It is even possible that this link is no more
existing in the last version of the page, so this click could lead to
ComponentNotFoundException if StalePageException is not thrown
earlier.
StalePageException just leads to render of the current page. So the
user will see the last version of the page and (s)he will need to
click the link again.
You need to use custom IRequestCycleListener (extend from AbstractRequestCycleListener).
Override #onException() and if the passed exception is StalePageException then you may use Session.get().error("...") to report that to the user. Just make sure you have a FeedbackPanel in the current page.
Would you wanna try a rather simpler approach by capturing the exception instance (lets say e). Then check for the type of this excception. And eventually use one of the inherited methods from java.lang.Throwable to capture the details
if(e instanceof StalePageException ){
e.printStackTrace(); //can use other methods as per desire : getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace
}
Please verify if getting the exception instance is possible for you to highlight in your code.

Strange behaviour with Navigate().back()

I'm facing an issue about the back function in selenium.
When I call it, it does nothing but if I call it twice, it works perfectly.
I've tried manually on my web browser and one back button is enought to go back.
Someone know why?
Thanks in advance!
The behavior might be a result of a scripted page with some code for auto-refreshing, suppressing or overriding the back button event /action.
So you might:
debug the page and stop on any JavaScript execution
and ask for a correction/change if you are in control of the page
avoid the back-button-action from Selenium-framework, that simulates browser back navigation
use instead a given button (if available)
implement this "feature" in your own framework as a workaround to click it (always) twice to ensure successful back-navigation

Selenium Webdriver.load.strategy unstable

I'm having problems loading my page with webdriver. My current (problematic) solution involves using the unstable load with firefox, but I'm open to other solutions.
The Core Problem
The root of all my problems comes from the fact my page will never fully load when I call it normally with webdriver, and thus will never preform the 2nd step, it's just always loading. It loads fine when you just go to the site with a normal browser. I've tried out a few work arounds that work intermittently, including opening the driver to google, and then going to the page which sometimes makes it load, and with
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("return window.stop");
as suggested by this question, which sometimes makes it continue without loading. But neither of those work consistently at all (probably <50% of the time)
The best I've got so far is using the unstable load strategy. However that has its own different problems, all of which only happen sometimes.
1) Sometimes it doesn't wait for the page to load at all, and I get an "Unable to locate element:" exception within milliseconds of loading the page, despite the fact that I have a wait set up:
new WebDriverWait(cdriver, 30).until(ExpectedConditions.visibilityOf(cdriver.findElement(By.id(myId))));
Thread.sleep(3000); solves that problem, but I've read that is a sloppy way to do things.
2) If I get passed that step, my test should click one link, then click another and continue on with the test. However, it often gets stuck after that first click. So if my code is like this:
Thread.sleep(3000);
element1.click();
System.out.println("clicked!");
element2.click();
The first click (which doesn't load a new page, by the way, just a pop up on the same page) will work, but then the system will never print out "clicked!", it's stuck in the same way would be loading the page initially (without the unstable load thing). If/when it makes it over that hurdle, I think the rest of the test is fine.
Any ideas 1) why it works sometimes but not others. 2) how to fix it 3) how to just get my page to load in the first place
Thank you!
Try the following approach:
Don't wait for visibility of element on the page - in your case By.id(myId).
The element can become visible much earlier than the page is fully loaded, before all java scripts are loaded and fired etc.
Wait for some element on the page to be clickeable instead. For example you can wait until link becomes clickeable in this way:
By locatorOfLink1 = By.xpath(....); // By.id, By.name, By.linkText etc.
wait.until(ExpectedConditions.elementToBeClickable(locatorOfLink1));
If the above will not help for problem #2 (the webdriver gets stuck after the click), then send ENTER key to the link instead of the click:
element1.sendKeys(Keys.ENTER);

Action in fb:request form is always null

UPDATE: this has just started happening again, would appreciate any help.
I am using server fbml to render an Invite Friends iframe and it was working fine up until two days ago. Basically, after submitting the form, a java action should be invoked where I get all the id's of the user's invited.
However, I now notice that an "invite by email" popup is showing up and, most importantly, my java action isn't being hit at all. When I inspect the form in firebug, I notice that the action is always empty (though all other parameters are preserved correctly).
I've tried setting email_invite=false on the multi-friend-selctor but to no avail.
My code looks like the following:
<fb:fbml>
<fb:request-form action="http://localhost:8080/myAction.do?method=someAction" method="POST" invite="false" target="_top" type="invite" content="CONTENT_GOES_HERE" >
<fb:multi-friend-selector showborder="false" actiontext="Invite your friends to use our app." target="_top" email_invite="false">
</fb:request-form>
</fb:fbml>
</script>
Any help would be greatly appreciated on this.
UPDATE: this has just started happening again, would appreciate any help.
Thanks, gearoid.
Facebook have recently added an extra step into the multi-friend selector process, which is what you're seeing. There isn't, as far as I know, any way to stop this from appearing.
However, you should still be getting the result of the selector sent to you once the user navigates away from the "invite by email" popup.
Facebook's latest update can be seen in this status post.
To prevent that popup appearing add this attribute to the multi friend selector
import_external_friends="false"
Turns out this can happen a lot of different ways.
A) You can call FB.init multiple times
B) Call FB.init with the wrong key
C) You can supply a action that doesn't map to your current application on facebook
D) You or your staff can accidentially supply the same canvas/app url for two apps.
Most of of the solutions online point at A-C. When someone accidentally creates a second dev app pointing at localhost or 127.0.0.1 as the canvas url for multiple apps you admin you will get hosed. Took me hours upon hours to think to search our other apps. So i hope this saves someone a truck load of time. Cheers
I had an incorrect appId set in the fb-root javascript. This caused the error.

Categories