I'm building a system with a login. When I was just testing it on my own computer, it worked perfectly fine. Once I uploaded it to our server, we started running into the problem whereby ... it wouldn't log in in the morning, for a few seconds. Works fine the rest of the time.
I've only run into this yesterday and today, and I can only test it once each morning, so it's hard to give too many details yet. Here's the setup:
The front page is an html page using javascript and angular js. It brings up an empty frame, then makes an ajax request for the contents of the page. On any failed ajax request that has a return value of 'insufficient access priviliges' it will un-hide a div holding a login form. That login form blanks out the page and provides a submit button. Once the submit button is clicked on, it sends an ajax login request to the Java server. Once that request is returned, the page hides the login form again, and things move on as usual.
On the back end, we have a java server running something between 1.6 and 1.7 (yes, I know, I should know better - the server reports back that it's 1.7.001, but 1.7 functionality like string-based switch-case statements don't work, so we compile for 1.6 java when uploading. It's a known issue we're working on.) We're using Stripes to do the front-facing code. The database is a mysql database, located on the same server.
When this error occurs, it's always the first login in the morning. I load up the page, type in the user/pass, and click login. The page blinks and comes back to the login screen. I haven't cleared out the user/pass after a login yet, because we're still in a testing phase, so I can just click 'login' again, and again, a dozen times before it finally logs in. This problem has only happened twice: yesterday and today, the first login of the morning.
My co-worker had a similar issue with another similar setup on a previous project, same server. He said he believes the problem has to do with the sql server closing the connection without the java server realizing that the connection has been closed, but he's not sure, and doesn't know how to fix it.
I don't even know what other information to offer, what code to show, or anything like that. The best suggestion I can find anywhere is "Set up a scheduled event to make a sure-fire database request every couple of hours," but this seems so ... kludgy. I'd love to be able to test for a more definitive problem and solution, if anyone could help me out. If anyone can tell me what code they'd like to see to figure this out, I'll gladly paste it up. More information? Just ask. The scope seems so ... big ... that I don't know which pieces might be relevant.
Edit 1: Error code!
[ERROR] - Database Error 1 on verify
java.sql.SQLException: Already closed.
at org.apache.tomcat.dbcp.dbcp.PoolableConnection.close(PoolableConnection.java:114)
at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
Edit 2: Connection Code
This is from an object we keep specifically for all mysql database connections.
private static ConnectionSource coreConnection = null;
private static DataSource coreDataSource = null;
public static ConnectionSource getCoreConnection() throws SQLException, NamingException {
if (coreConnection == null || !coreConnection.isOpen()) {
Context env = (Context)new InitialContext().lookup("java:comp/env");
coreDataSource = (DataSource)env.lookup("webapps/test");
coreConnection = new DataSourceConnectionSource(coreDataSource, new MysqlDatabaseType());
}
return coreConnection;
}
Configure your connection pool to validate connections. The connection pool will then send a simple query (which you specify during configuration) to the DB. If that query fails, it will close the connection and give you a new one, otherwise it will use it in place. That helps solve problems like these as well as problems where the database goes down behind the back of the server.
MySQL closes connections which have been open for a long time, but don't do anything. From what you've described it sounds like this is what you're running in to.
The first login fails because it finds that the used database connection is no longer working, and the driver notices this as well. The driver will then use a new connection for the next request.
The first way to handle this that comes to mind is detecting this SQLException, and handling it by opening a new connection with the same query. There may be other solutions available within the driver that you're using, but unfortunately I am not aware of any.
To aid with detecting this SQLException you can take a look at the docs, and evaluate if the methods listed there return anything unique to this error.
If you are using a connection pool, then this question may contain the answer to keeping the connections alive.
Related
I have a java web app that is running inside tomcat and the application has a page (JSP) where users clicks (some buttons on that web page) which results in hitting a query behind the page and reloads it with new information and sometimes that query takes longer than expected and I want to kill the query automatically if it runs longer than x minutes. How can I achieve that at tomcat side, is that even possible with any tomcat server configurations like timeout or anything, if so could someone please guide me or show an example configuration or setting change I can try? Thanks
I think you are looking for Statement.setQueryTimeout which is a feature of JDBC, not of any specific application server, etc. You will want to call that method and set an appropriate timeout. Your queries will throw SQLTimeoutException if the timeout elapses and your application will have to decide what to do next.
Application - Struts 1.2, Tomcat 6
Action class calls a service which through DAO executes a query and returns results.
The query is not long running and gives results in seconds when run directly over the database (through SQL client say SQL Developer), but, when the user browses through application front end and same query is run in background through the application, the system hangs and the response either times out or takes a lot of time.
Issue is specific to one particular screen implying that app server to db server connectivity is ok.
Is there a way to enable debug logging of Tomcat/ Struts without any code change, to identify one out of the two scenarios below or any other scenarios possible?
The query is taking the time.
The response is not being sent back to the browser.
P.S. - Debugging or code change to add logging is not an immediate option.
Something to look at is a "Java Profiler". The one that I've used and have liked is YourKit.
In a recent job interview I was asked a J2EE debugging question. The question was As Follows:
"You are not getting same data as expected from your server how do you debug it?"
What or how should I answered this question that would make the interviewer happy??
Please suggest....
On the top of my head, usually you would
check the request and compare it with API - is the request being done correctly
check the logs for any problems on the server
confirm that the version of server application matches the one expected
check the database data status
if else fails, try to reproduce the problem locally or in a lower environment or step through the server app execution path with a debugger. Increasing the log level or hooking up to debug interface might be relevant as well.
When you have to debug code to server there is a common way on how to debug the code. You see server logs. Now, if you cannot find any errors, you have to see what the API returns at every single step. If you have not logs for every step, put log.debug("Some text that means something"); and rerun. If something is unusual, then you have to check the specific step.
The question is too general and opens a way for you to literally bombard the interviewer with questions and that's probably what he expects you to do.
Usually when sh. hits the fan I want to know: does the user receive any data at all? If not, app log, server log and db log is where I look. Some apps we use got app log located on the disk where the app is running, some are using db based logs and some are using the default log which can be accessed through admin console on the server (Glassfish for example).
On the other hand if the user received incorrect data I start tracking through the app how the data is "made" which usually means going through several db queries and such where I'm trying to determine what's going on. After that I compare the result I expect with the result user received and according to the difference I decide what went wrong.
But hey this question is too general in this environment so you either let the interviewer specify the problem or create your own scenario for him.
I don't if the question title fits, but here is my problem:
I have a regular webhosting service in hostmonster, with a website built in php.
So I have php script running in a cron job that monitors a xml file for changes, and everytime a new entry comes into that xml file the script stores it in a database.
In the other hand there is java built desktop client, which needs to be noticed ASAP that a new entry is created, for this the client connects to a second php file every second, and this second files tells if there has been changes or not.
The thing is, every 260 connections my I.P gets banned from the server :( and the client crashes, the client will be used by several users.
I contacted support on how to handle this, but they tell me to use a single connection, I tried reusing the UrlConnection but after the first request it just gives null. then I tried with Sockets but no luck. I know there are libraries that manage this but I dont know how are they called. Can someone give me advice?
thank you guys.
Use a long polling method. Hold the connection opened until response arrives. This way you only need to ask for the update once.
PHP may not be the best tool for this job though.
I'm working with Apache Tomcat 7 (JSP and Servlets). In my application, I need to send some messages from server to client. Bellow, I'll explain a little bit what I'm working on.
Brief explanation: The application will bring up a login page if the user isn't logged in every time when he wants to connect to internet. After the user logged in successfully and his time is going to end, I will need to send to client a message with remained time (for example in last few minutes). It can also be another requirement to open advertising popup at a specific time.
I now about JMS but I don't know how fit is that for my scenario. I also read in other posts, the WebSocket can be also an option.
I'm running the server on CentOS 6.2.
Question: For this scenario, do you have some thoughts on how to treat it with Java technologies? If you have some other ideas, feel free to expose!
N.B. Related to JavaScript and PHP I found good answers on SO's questions. I'm interested on how to solve this issue with Java technologies especially.
http://jwebsocket.org/
Maybe this fits your needs.
You will not be able to initiate an HTTP connection from the server to the client. One solution will be to use WebSocket/Comet Framework. Unfortunately websockets are not really wide spread (server+browser) for now. I will suggest you to use a framework to fill the "gap": https://github.com/Atmosphere/atmosphere
I don't understand your obsession with us implementing the solution in Java - any valid solution should be portable across different serverside languages. However if the termination is to occur without synchronous user-driven interaction, then you're just creating load on your server by trying to handle it here. If you want somebody to write the code for you then this isn't the right forum.
I now about JMS....CentOS 6.2.
Not much help here.
The thing we really need to know is what you mean by:
After the user logged in successfully and his time is going to end
(I assume you mean the session time is going to end, unless you've written some software which predicts when people will die).
How do you determine when the session will be ended?
Is it a time limit per page?
Is it a fixed time from when they login?
Is it when the session is garbage collected by the Java?
In the case of 1, then the easiest way to achieve this would be to use javascript to set a timeout on the page (when the user navigates to a new screen the timeout will be discarded), e.g.
setTimeout(function() {
alert('5 minutes has expired since you got here - about to be logged out');
}, (300000)); // 5 minutes
In the 2nd case then you'd still use the method above, but reduce the timeout on the javascript by the time already spent on the server (write the javascript using java, or drop a cookie containing the javascript timestamp at login).
In the 3rd case.....you don't really have any way of knowing when the user will be logged out.