This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Multiple clients Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple clients" Watch "Multiple clients" New topic
Author

Multiple clients

Shanthi Murugeson
Greenhorn

Joined: Dec 30, 2005
Posts: 10
Hi everyone,
we have developed a small application using servlets and jsp. our application works fine when a single client tries to use it but when more than one client tries to access the same application, two exceptions are thrown
1.Protocol violation
2.Invalid arguments in column
i could not understand why this is happening can anyone please explain what could be the problem? and what should be done to enable multiple clients to access the application at the same time.

Thank you
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

We would need a lot more information.
You shouldn't need to do anything special in order to support multiple users.
Can you post the stack trace and the code for the servlet that's blowing up?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Thibault Dangr�aux
Greenhorn

Joined: Dec 12, 2005
Posts: 19
The first things to check is whether your servlets/controllers (and the services and POJO used by them) are thread-safe *and* stateless. Without looking at your code, a problem that only occurs during concurrent accesses would suggest something like this...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a very easy mistake to use member variables in a servlet which is not thread safe. You can try to turn all those into local variables and parameters in the doWhatever methods and the methods they call. That can be a bit ugly. Or you can take all the code out of doPost or doGet and put it into another object which you make thread safe.

Worker can safely use member variables because we make a new instance for every request. This defeats the container's effort to reduce object creation and GC by reusing a single servlet instance, but as long as the Worker is cheap to create it's not too bad.
[ January 25, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Shanthi Murugeson
Greenhorn

Joined: Dec 30, 2005
Posts: 10
hi,
this is the stack trace i am getting...


java.sql.SQLException: Protocol violation
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:1160)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:594)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1983)
at oracle.jdbc.ttc7.TTC7Protocol.fetch(TTC7Protocol.java:1250)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:295)
at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_OracleResultSetImpl.next(Unknown Source)
at DatabaseServlet.doGet(DatabaseServlet.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:635
0)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
java.sql.SQLException: Io exception: invalid arguments in call
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:334)
at oracle.jdbc.driver.OracleStatement.freeLine(OracleStatement.java:6502)
at oracle.jdbc.driver.OracleConnection.needLine(OracleConnection.java:2415)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:291)
at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_OracleResultSetImpl.next(Unknown Source)
at DatabaseServlet.doGet(DatabaseServlet.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:635
0)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

and the line corresponding to "DatabaseServlet.doGet(DatabaseServlet.java:97)" is
rs = dmd.getColumns(null,null,tablename,null);
where rs is the resultset, dmd is the object of databasemetadata and tablename is the name obtained from the jsp page.

please help me..
thank you..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you declared any of the database variables (connection, resultest, etc..) as instance varaibles?
Shanthi Murugeson
Greenhorn

Joined: Dec 30, 2005
Posts: 10
yes, i have declared my connection variable as an instance variable.
infact all the variables in the program are instance variables.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Shanthi Murugeson:
yes, i have declared my connection variable as an instance variable.
infact all the variables in the program are instance variables.


The connection object is not thread safe. Create a new instance (or get from connection pool) for each request.


Groovy
Shanthi Murugeson
Greenhorn

Joined: Dec 30, 2005
Posts: 10
this is my code..



[ February 06, 2006: Message edited by: Shanthi Murugeson ]
[EDIT - code tage added by Dave]
[ February 06, 2006: Message edited by: David O'Meara ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yep, your SErvlet is not thread-safe. Servlet containers only create a single instance of a Servlet and run multiple requests through it. If you have instance variables like you have above, each of the threads can change it to a different value while another thread is using the old value, hence not thread safe.

Remove all of the instance variables for a start
Shanthi Murugeson
Greenhorn

Joined: Dec 30, 2005
Posts: 10
I thank everyone for your replies..

It is working fine now.. I just need a small guidance. Can you please tell me where should we establish our connection and release all the connections. Previously we had all these in the init() and destroy() method.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
You can implements javax.servlet.SingleThreadModel to mark your servlet to use single threading model. This is not recommended and performance impact is huge when service method execution is long.

You can open and close the connection in the service method if the connection is managed by a connection pool. or else you can just leave it open to save creation time later.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Eddy Lee Sin Ti:
You can implements javax.servlet.SingleThreadModel to mark your servlet to use single threading model. This is not recommended and performance impact is huge when service method execution is long.

.


SingleThreadModel is deprecated. So avoid that API and also it not really useful.
You can open and close the connection in the service method if the connection is managed by a connection pool. or else you can just leave it open to save creation time later

What do you mean by keeping it open ? Are you suggesting to use instance varibale for connection objetc. It is not thread safe.Connection objects should be closed when done using it whether one uses Connection pooling or not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple clients
 
Similar Threads
Recognizing the Owner of a Lock
Message Driven Beans
Multiple Instances of Standalone
Requests to Servlets
EJB locking