I currently have the following situation:
I have build a client Java application, which used to communicate with the MySQL database directly. When I was inserting a lot of data, I used to do this with AutoCommit turned off. This was easy to do, because I had direct access to the Connection. I had chosen to do this, because it enabled me to rollback the changes when something went wrong during the synchronization.
Now, the application is evolving and I thought I would be better to build a server API which communicates with the MySQL database. So, at the moment, I am inserting data by doing Http requests. Each request opens and closes a new Connection.
Now, I would like to be able to rollback the changes when one of the requests goes wrong. I assume I can not work with AutoCommit, because this is based on a Connection, and that one is different for each request.
Can anyone tell me how this is done usually?
I have thought of the following:
First call an URL which sets the AutoCommit to false, and then do all the requests and check if one fails. But this would go wrong if another client is also inserting data at the same time.
Sending the data to the server with one request, but this would force me to alter my design drastically.
Note: I know some code is usually required when asking a question, but I can not see how this would improve my question. However, if it is needed, feel free to request it.
Every web request should run in it's own transaction.
Send all the data that belongs to a logical transaction in one request and call commit at the end of the request processing.
Spaning transactions accros multiple web requests is a bad idea. Think about crashing clients which never commit. This will leave you with open transactions that never close.
Related
I have two war files such as war1 and war2
If am login the application, the session will be created in war1 and from that if am navigate to war2, there i need the same session data.
I tried crossContext=true in context.xml of server from that i can access the data by storing it in servletContext.
But the issue is once i logined the screen in chrome the session data will be stored in servletContext and the data will maintain till the application is running.
If am giving the same URL in another browser like IE here also, i can get the servletContext data so instead of navigate to login page the corresponding screen will be opened
Kindly suggest me how can i overcome this issue in java?
Is there any way to findout browser switching or incognito window mode of the browser in java?
Note: am using tomcat server
I have never dealt with your exact configuration problem, but even if you can make this work on a single Tomcat instance, you might have problems should your two web applications ever be distributed across multiple Tomcat instances.
So, I am going to suggest that you actually use a database to store state which needs to be passed between the two applications in a safe and reliable way. Note that the database approach also scales nicely in a distributed environment, so long as you have a single logical database.
While session replication indeed can be done in Tomcat (see here) I really suggest you to avoid this type of issues by eliminating the session altogether.
This session replication is an approach that was somewhat common before ~15-10 years, but nowadays when we have a lot of servers running in parallel to serve user requests and have elastic clusters, this approach is not good enough because basically it doesn't scale well.
There are many ways to achieve what you want, though:
Use a shared database to store the session information. Add some session Id to the response and require the client to pass this id back into all subsequent request along the session. Then execute a query to the Database by this Id and retrieve all the session information.
This solution also doesnt really scale well, but then you can shard the session information if the db permits to do so...
Use Redis/Aerospike to save the session information of the currently connected user. somewhat like DB approach, but since redis run in-memory it will be much faster. In general, this approach can be used in conjunction with 1 where redis is an in-memory cache.
Encrypt the session information or even just sign cryptographically and send back to client. Client will have to supply this information along with the request without knowing which server will actually serve this request.
Without delving into cryptography I'll just state that encryption can be done if you don't want client to see the session information (despite the fact that this is the user whose information is supplied) and signature is used to prevent tempering the data (while sending it back to server).
The data can be supplied to server from client via Header or cookie for instance.
I know I don't have any code to show you guys, I am stuck at some point and I dont know where to start. I hope someone will help me.
I am developing a Spring MVC application, and I need to send a message to all active session users to execute a script which is available to all the clients as it is defined in a js file and included for every user.
I have looked around and found some frameworks which offers these type of functionalities like Atmosphere but I don't think it should be used in my situation as it is a big framework and the functionality required is very little. I have also gone thorough WebSockets but I cant find anything which would help me in invoking the script on client side for all the clients.
If someone can help me go to a right path or direct me to a similar example. I will be grateful
****Update****
I can also use polling if there is way that: if the controller gets a request the session should be considered idle during that, for instance, there is controller which is called every 5 minutes and in the session out time is 30 minutes. The session won't expire in this time if the controller used for polling is called every 5 minutes, I need to exclude the particular controller from calculating the idle time
No Polling Solution:
From what I gather, you need a Remote Procedure Call mechanism.
I would go with https://github.com/eriksank/rpc-websocket.
The general idea:
Your clients register themselves to your server process as "consumers".
When a message is ready, your server then goes through every registered "consumer" and sends the message which rpc-websocket can handle .
Polling Solution:
Here is a general idea, works if you have registered, logged on users.
Have a database table that stores messages, lets call it "messages".
Have a database table that keeps track of messages and users, lets call it "message_tracker". if a user has seen a message, there will be a row in this table for the messageId and UserID.
Have a javascript script poll a server url for new messages. What is a new message can be decided based on the database tables above.
If a new message is found, process it and then call another server url which inserts into the message_tracker database table
i would like to know how the update messages in this stack overflow site have been implemented.
To be more precise, for example while i am trying to reply for question and i am in the middle of typing my response, i will see a message on top of the page saying a new answer has been added. How is this feature has been implemented.
AFAIK, the possible way can be HTML5 websocket or serversocket technology.
is there any other way to achieve this kind of push notification system especially using java, spring and jquery environment?
Not sure how to tag this question. correct the tags if i am wrong.
SO uses reverse ajax/comet technology to show those messages. I remember reading some discussion on meta about this feature, couldn't exactly find out the link for it at this moment. Will update as soon as I find.
Based on programming language framework name may change (websockets (or) socket.io etc.,), but at the end they all are from comet framework.
Update:
Here is SO meta discussion on this topic.
I have used the Direct Web Remoting framework with success. (DWR).
There are several ways to achieve that:
Polling: using JQuery you issue a request regularly (every 5sec for examples) which retrieves the updates from the server.
Streaming: you issue a request, the server does not set a Content-Length to the response and "never" close the socket. This way you can send data from server to client whenever you want. But it means that for every client a connection is hold by your server.
Long polling: mix between the two previous ways. The connection is hold by the server but with a timeout. If no new data is available, the server closes the connection and the client reopen a new one after a moment.
Thoses are Push technologies: http://en.wikipedia.org/wiki/Push_technology
Of course there are over ways to achieve that.
Environment: The application is using Spring Framework 2.5.6.SEC01 and iBatis 2.3.4.726. It is MVC design.
Here's the scenario:
Input/update data from the client
Press Update button to submit
Process the data and execute DML (insert, update, delete)
Back the result to client and display the data
However, upon the page loaded, I need to call the API via Javascript (i have no control with the API, just need to pass the required parameter and check the result if SUCCESS or ERROR)
If API returns SUCCESS, nothing to do. But it returns ERROR, I give alert message to inform the user.
I have View(client), Service and Data Access Layers. When the client do the submit (scenario #2), it enters the Service to process the data and automatically start Transaction (scenario #3). Automatically execute the commit upon exit to Service and back to client to show the data (scenario #4).
Problem: How can I suspend the transaction not to execute the commit, then back to client to call API via Javascript. When API returns SUCCESS, execute commit via Ajax (or other way) or in the other hand, rollback it.
Any guidance on the right direction is appreciated.
If I understand correctly, you want to start a database transaction, insert data (without comitting), keeping the connection and the transaction open, return to the client, and based on some javascript result, do a commit.
This feels like a strange design where the client can actually keep a connection open, making your application extremely vulnerable for (D)DOS attacks or client problems in general.
I would try really hard to remodel it as follows:
Upon submit, call the javascript you need to confirm the commit/save action
When the javascript succeeds, do the submit to your own server
Do normal Connection/Transaction handling within the DAO, not exposing transactions to the client.
This is quicker, more robust and also probably less code.
Hello, it's really the first time for me to post.
I'am encountering a problem with my Web Apps and cannot find any answers on the web.
My problem is that i have a Java Web
App that works on parallel with
tomcat and apache using mod_jk.
Everything works fine, but after one
day of running in tomcat, the Ajax
request i do with Jquery dosn't come
back with the data. I'm using SQL
Base (phpMyAdmin).
I'm sending a request to the servlet
in charge of the sql using Jquery Ajax but i
never get the response. Wierd thing
is that it works for a day and then
stop the next one (i must than reload
my WebApps to make it work again).
Could you gentle developpers give me a hint or two please ? :)
I think whenever you are opening the connection to take data from database then probably you are not closing those connections.And every time your application is creating new connection with database without closing old one.As sql has limited number of connections so after one day or after some time your application is not able to connect with your database.So you are not getting data from database. And when you are reloading your application that time you are restarting tomcat so that time your all connections with database are being closed.So next time again you are able get data from database.
So check whether you are closing database connections properly after every transaction or not.Might be this is your problem.
Have you looked at any error messages in the log files? Especially, do you know if the corresponding Servlet gets called at all, or is the request blocked by apache / tomcat?