|
|
| [Java/MySQL] "No operations allowed after connection closed." |
|
Schyfis
Member #9,752
May 2008
|
I just finished a web application that is supposed to be able to connect to a database and perform various operations. It works great when I run it locally on my computer, but it's exhibiting odd and erratic behavior when run through a web browser. I get this error, and no database operations work. How can the connection be closed, especially after I just opened it when the program started? I explicitly open the connection when the program starts, and explicitly close it when the applet is destroyed. What's going on here? ________________________________________________________________________________________________________ |
|
ReyBrujo
Moderator
January 2001
|
I got that error on .NET a couple of times, all involving accessing a recordset that was closed by the time I tried to read from it due a synchronization issue. Are you sure it is not throwing an exception when trying to connect the second time you open the tab? Add a delay before opening the second tab, or clear the cache; maybe the browser is trying to reuse the old applet and its connections the second time. -- |
|
Archon
Member #4,195
January 2004
|
Schyfis said: How can the connection be closed, especially after I just opened it when the program started? Check which Applet methods you are doing the opening/closing in. There are like four in total relating to applets -- two for opening, two for closing, but at different scopes. My suspicion is that you're opening it in the correct method but closing it in the wrong method. |
|
Schyfis
Member #9,752
May 2008
|
Archon said: My suspicion is that you're opening it in the correct method but closing it in the wrong method. I was using destroy, but closing it in both stop and destroy doesn't affect it. ReyBrujo said: Are you sure it is not throwing an exception when trying to connect the second time you open the tab? Pretty sure. Any exception is caught and printed with Exception.printStackTrace. After opening it a second time and getting the error, I closed the tab and garbage collected from the Java console window. After that, I was able to use it again. Edit: Edit 2: It seems that my error checks don't work for some reason. public void createTable(DBTable table){ try{ //try to create the table (connection has already been made) } catch(MySQLNonTransientConnectionException e){ instance=new DBConnection(); //something weird is going on, so reset the singleton createTable(table); //and call the method again. } catch(Exception e){ e.printStackTrace(); //for other errors } } It never catches the MySQLNonTransientConnectionException. Edit 3: Edit 4: public void stop(){ if(!DBConnection.dead){ DBConnection.instance().closeDatabaseVariables(); //close the connection, statements, and resultsets DBConnection.instance().die(); //set singleton instance to null } //garbage collect Runtime r = Runtime.getRuntime(); r.gc(); super.stop(); } //destroy is the same Any ideas? ________________________________________________________________________________________________________ |
|
ReyBrujo
Moderator
January 2001
|
Wait a while before opening a new tab and trying again. Also, can you check how many database connections you have the second try? None? One? Two? The singleton looks like it doesn't create a new instance because it still exists. -- |
|
bamccaig
Member #7,536
July 2006
|
I'm not very familiar with Java and have never written Java-based Web applications or Java applets, but I remember from The Clean Code Talks - "Global State and Singletons" that static members are shared by the entire JVM. In other words, they are stored on the class, not in the application, and subsequent runs of a given program will still share that same static value. So, while you may think your Singleton is new every time you access the page, it might very well be carrying over. It seems reasonable that a single JVM would be used for many applets. In other words, the browser probably wouldn't kill the JVM until it was sure no more applets were likely to be used and on subsequent accesses to the page would continue using state from previous executions (I'm purely speculating as I know very little about Java and even less in regard to applets). Rather than using a Singleton, you could try taking the advice given in the Google Tech Talk (which specifically mentions Java applets, IIRC) and rid your application of Singletons. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Schyfis
Member #9,752
May 2008
|
I was afraid that was happening. What's going on here? I can't test it if I can't load it. For example, I upload version 1 to the server and test it. Is there some other cache I need to clear? Or can this be fixed with HTML no-cache meta tags... I'll try that next. Edit: Edit 2: Edit 3: ________________________________________________________________________________________________________ |
|
bamccaig
Member #7,536
July 2006
|
Schyfis said: But now that I've disabled it, how can I keep the same problem from happening on other computers? http://java.sun.com/products/plugin/1.3/docs/appletcaching.html
Schyfis said: This is rapidly becoming a nightmare.
Now you're beginning to experience Java. Schyfis said: A simple program that works perfectly on my hard drive refuses to work online. Have you tried searching the CLASSPATH for that class on those other computers?[1] How is the CLASSPATH even set for applets? ** EDIT ** One Google result suggests wrapping your applet into a jar and creating a META-INF/MANIFEST.MF file with the applicable Class-Path entry. Then, use the archive attribute of the <applet> tag (apparently <applet> is depricated though, so here are instructions for using archives with the <object> tag) to embed the Java applet. My question then becomes what should you expect to be installed on the client and what should the client bring down from the server? Should standard-library code be expected on the client while all else brought down from the server? Should the class path first check on the client and then revert to the server? I don't know, but I'm sure there are best practices. References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Schyfis
Member #9,752
May 2008
|
bamccaig said:
Now you're beginning to experience Java.
I've been working with Java for two years now and this is my first web application. I never believed it could be such a hassle to get things working! There are so many nuances and security restrictions that I almost considered leaving it as a desktop app. Anyway, I redesigned the program without singletons and it looks like everything is finally working. I solved the NoClassDefFoundError by uploading the mySQL connector jar and referencing it in the object tag as follows: So for the no-cache options in the object tag, I want something like this, right? <param name="cache_option" value="No"> <param name="cache_archive" value=""> It said both parameters were required or it would revert to the default. Also, thanks for the video. That helped explain a lot. ________________________________________________________________________________________________________ |
|
ImLeftFooted
Member #3,935
October 2003
|
I figured it out! ... ... its cause you're using java. |
|
Schyfis
Member #9,752
May 2008
|
Dustin Dettmer said: its cause you're using java.
This time around it just happened to be the right tool for the job. It was just a dull, rusty, nonfunctional tool. ________________________________________________________________________________________________________ |
|
gillius
Member #119
April 2000
|
Since you have some experience with "desktop" applications and considered that, did you consider using webstart? Web start Java apps are a sort of cross between applet and installed application. .NET ClickOnce is very similar or completely the same. The user starts the application in the browser, but then it is downloaded and run similar to a desktop app, although still within the security "sandbox" (SecurityManager). Web start process handles the update process very robustly, and can even be "installed" as a shortcut to the desktop, but I think this requires Internet connection to your application every time it is run to check for updates. Gillius |
|
|