I have a web app on Tomcat, which handles DB connection pooling, and using Spring JDBCTemplate for executing queries. It's been requested that I implement a status page which will be monitored by a heartbeat process to determine if everything is healthy with the server.
As part of this, I want to do a DB query to determine if the connection to the database is ok. Ideally, since it'd just be a 'select 1 from ', I'd want it to come back fast, within 10 seconds, to indicate a failure if the DB didn't respond in that time.
However, I don't want to change my connection to time out that quickly for normal requests.
Is there a way to set a per-query timeout using either raw JDBC or Spring JDBC wrappers?
Use setQueryTimeout on the Statement (or PreparedStatement) object.
If you are using spring to manage transactions a time out can be specified at the transaction level as well. #Transactional(timeout=10)
Related
I have an api exposing a websocket connection and to keep the connection alive my reactjs frontend echoes in the websocket connection each second. Whenever the server receives the message, a database query (a SELECT) is done. So I'm querying the database each second by the way. Will it kill the system overtime ? Is it a poor practice to query a database as frequently as that ? Any explanation would help me improve the code. My system will go production in a few and I'd like not to encounter any silly problem
According to your words, a query is executed every second, and by doing this, you will have problems with the server resources
In my opinion, you can have two different solutions
1- Manage the number of requests from the database using the design pattern and data caching
2- Change your websocket structure and in case of an event or data changes, take the data from the bank and send it to the user.
In Active JDBC, is there a functionality that allows us to set Connection Time Out limit ?
It works like... whenever the user deletes or (insert, update, etc..) a large number of data and suddenly the server's connection is lost... it will rollback its transaction if the time of waiting is greater than defined time out limit ???
Regards, Vincent
Found this : Base.connection().setNetworkTimeout(); but... No documentation on ActiveJDBC. Does this still work???
This methos is not a function of the framework. The code in question:
Base.connection().setNetworkTimeout()
relates to java.sql.Connection, which is part of JDK/JDBC:
https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#setNetworkTimeout-java.util.concurrent.Executor-int-
As such, you can find documentation there. However, I would recommend you to NOT track timeouts but run your statements under transactions. This means that any time you have any error, including network failures, your data integrity will be preserved. See: http://javalite.io/transactions.
I have a ASP .NET web service that leverages a long lived connection from the client.
The client connects in and waits for 15 minutes for a response.
Just prior to 15 minutes, the ASP .NET Web Service responds with an OK.
The client repeats this connection establishment.
During the 15 minutes, the Web Service checks for a change in a field value in a record in an SQL table. If that value changes it then immediately sends a response to the client with ReadMessage. This checking / polling of the database is done every 30 seconds. This has several drawbacks:
it does not scale well. It works well with 1 or 2 clients, but when you end up with 10,000 client connection that is a lot of polling on the database.
It leads to latency in processing as it may take up to 30 seconds for the client to be notified.
What I would like is to find a way of notifying the Web Service for the active http client that the record has been updated.
It should also be noted that each client connection to the web service has it's own specific record in the table.
I think SqlDependency is what you are looking for. Query Notifications allow applications to receive a notice when the results of a query have been changed
Have you considered setting up some triggers in the db? If you are using SQL Server you can use SQL Server CLR integration.
http://msdn.microsoft.com/en-us/library/ms254963%28v=VS.80%29.aspx
You could put a trigger on the table. Disclaimer: I try to stay away from triggers because it's very easy to write one poorly and when it errors it's hard to debug. However, I haven't ever written a CLR trigger and I imagine there's a little more safety in that since you have more control over error handling.
But even better would be to have whatever process is updating the table to begin with notify your webservice of the change if that's an option.
I have an extremely simple BizTalk orchestration that takes a HIPAA 837 file in, breaks it into its individual claims, and saves the complete xml message to the database. I have a WCF SQL send port that calls a stored procedure to do this... the proc just does an insert with no return value. The problem is that I keep (randomly) getting the timeout error:
Details:"Microsoft.ServiceModel.Channels.Common.InvalidUriException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
I just tried this with a small file - only 5 individual claims in it (so I should only need 5 connections from the pool, right?). The BT server has been doing nothing else for the past 10 hours (no messages processed). Yet I still received this error... My MaxConnectionPoolSize is set to 100, so that means 100 connections have been held open and idle for at least 10 hours ?? What's going on here?
Thanks.
I would take a look here or here. To be honest the WCF SQL adapter is very picky and quirky as to what SQL it works well with and what it doesn't. I typically look for a custom solution for inserting into SQL to have more control over the inserts or updates without having to write my SQL specific for the SQL adapter. I find if I'm inserting or updating more than one table or returning a complex records, I avoid the WCF SQL adapter.
If that's not an option, look at re-writing your SQL.
I am making a Windows service which needs to continuously check for database entries that can be added at any time to tell it to execute some code. It is looking to see if it's status is set to pending, and it's execute time entry is > than the current time. Is the only way to do this to just run select statements over and over? It might need to execute the code every minute which means I need to run the select statement every minute looking for entries in the database. I'm trying to avoid unneccesary cpu time because I'm probably going to end up paying for cpu cycles on the hosting provider
Be aware that Notification Services is only for SQL 2005, and has been dropped from SQL 2008.
Rather than polling the database for changes, I would recommend writing a CLR stored procedure that is called from a trigger, which is raised when an appropriate change occurs (e.g. insert or update). The CLR sproc alerts your service which then performs its work.
Sending the service alert via a TCP/IP or HTTP channel is a good choice since you can deploy your service anywhere, just by modifying some configuration parameter that is read by the sproc. It also makes it easy to test the service.
I would use an event driven model in your service. The service waits on an auto-reset event, starting a block of work when the event is raised. The sproc communications channel runs on another thread and sets the event on each incoming request.
Assuming the service is doing a block of work and a set of multiple pending requests are outstanding, this design ensures that those requests trigger just 1 more block of work when the current one is finished.
You can also have multiple workers waiting on the same event if overlapping processing is desired.
Note: for external network access the CREATE ASSEMBLY statement will require the PERMISSION_SET option to be set to EXTERNAL_ACCESS.
Given you talk about the service provider, I suspect one of the main alternatives will not be open to you, which is notification services. It allows you to register for data changed events and be notified, without the need to poll the database. It does however require service broker enabled for it to work, and that potentially could be a problem if it is hosted - some companies keep it switched off.
The question is not tagged to a specific database just SQL, the notification services is a SQL Server facility.
If you're using SQL Server and open to a different approach, check out SQL Server Notification Services.
Oracle also provides notifications, the call it Database Change Notification