Connection to database lost after several hours in Grails war - mysql

I'm having a rare issue with a war I deployed, and it's connection to the database server (MySQL), here's the thing:
I made the tomcat config like this:
<Resource name="jdbc/gimnasioBackend" auth="Container" type="javax.sql.DataSour$
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username=["username"] password=["password"] driverClassName="com$
url="jdbc:mysql://localhost:3306/[databaseName]"/>
And is connecting just fine, but the thing is that every day I come to check the server status, and also test the pages in it, I can't login. If I restart the server (which is not recommended, judging what I've red), I can login (using spring security) again, but not for long... The situation is complicated, mainly because it's already in production, and is holding a backend for an app..
Thanks in advance!
Note: There's more than one web app in this tomcat, all of them working fine.

I had a similar issue with grails DataSource and with tomcat resource configuration connecting to an Oracle database and I resolved adding
validationQuery="SELECT 1 FROM DUAL"
validationQueryTimeout=3
validationInterval=15000
If the problem is due to connection against database lost I suppose you can try adding those properties. Validation query for mysql should be select 1 instead of select 1 from dual

Related

How to fix Named Pipes Provider, error: 40 - Could not open a connection to SQL Server?

I'm having trouble connecting or logging in to my SQL Server database. I had a previous working instance before, but I forgot the password and decided to just reinstall the whole thing since I don't have any data to lose. I realized that was a bad idea but now, each time I attempt to log in to my SQL Server, I get the following error message:
I tried to search for an answer, but it seems nothing is working. Here is what I tried so far:
Enabling the Named Pipes from the SQL Server Configuration Manager under the SQL Server Network Configuration tree item
I tried restarting the SQL Server item from the list of services in the Services application
I tried restarting everything in the SQL Server Configuration Manager that is related to my database or at least have my instance's name on it
There are other things that I tried which I fail to remember. Any help would be much appreciated.

Pomelo MySQL (.NET Core) Can't Recover After Database Failure

Last night AWS RDS had an "Internet Connectivity Issue" that was resolved a short time later. However, my app (which runs in .NET Core and connects to an RDS MySQL instance via Pomelo.EntityFrameworkCore.MySql) could never re-establish a connection to the database even though the MySQL server was back online. I tested connecting from my own local machine and it worked just fine. I then re-deployed the .NET Core app it everything started working again.
Is there something that I need to re-create (the db context perhaps), or is there something that is being cached that I need to flush to try to connect again? I connect via hostname, and my connection string looks something like this:
server=something.somewhere.us-east-2.rds.amazonaws.com;userid=XXXX;password=YYYYY;database=ZZZZ
Here is the Exception being thrown:
MySqlException: Unable to connect to any of the specified MySQL hosts.
at MySqlConnector.Core.ServerSession+<ConnectAsync>d__56.MoveNext (C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:239)
and here is how I create my db context in Startup.cs:
services.AddDbContext<BlayFapContext>(opt => opt.UseMySql(Settings.Instance.SQLConnectionString));
Any help would be greatly appreciated.
Giawa
Okay, we worked out what happened. Pomelo's MySQL wrapper had an issue as outlined in their git repo here: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/434
Basically, if a MySQL database is not available when the connection string is first used then it will be cached as invalid and will never work again. You can easily confirm this by launching a service with no MySQL connectivity, verify it doesn't work, then launch MySQL and confirm that the service still doesn't work. It can never establish a MySQL connection after the first connection string is found to be invalid.
They patched it shortly after the 2.0.1 release, but they haven't updated Nuget with a new version since then, despite the issue being found 6 months ago. So, the fix is to checkout their repository source code, and patch it ourselves. We found the fix here works just fine: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/pull/456
So, why was the connection string retried? We already had a successful connection! It turns out that the internet connectivity issue with the Ohio data center was not limited to RDS, but also affected EC2. Our EC2 instance was rebooted as part of the fix, and the MySQL connection wasn't valid when it reboot due to the continued connectivity issues. The state of that connection was cached, and even though the MySQL server came back online our service was toast.
Giawa

Unable to connect to any of the specified MySQL hosts in NHibernate, MySql

One of the client is reporting this issue happening in our application. NHibernate throws Unable to connect to any of the specified MySQL hosts in the middle of the app running, so it is not a mistake in the configuration. It happens randomly, but always when the transaction is opened.
I saw this:
NHibernate, MySQL, Windows Server 2003 -- connection problems
But the server in my case is Windows 2008. And app opens only single connection simultaneously. MySQL version is 5.5.28. Server is on the remove machine
Unfortunately the app requires the HW I do not have, and I cannot reproduce it locally.
Any ideas or directions I need to dig in?
Try connecting using the full directory name, e.g. instead of Server=SRV use
Server=SRV.yournetwork.com
It might be that is has trouble resolving the server name.

Java web application running on Tomcat unable to execute SQL queries with DB Connection pooling

I've come across similar questions many times here on Stack Overflow, however, I haven't been able to get it right.
I'm trying to use Database connection pooling in Tomcat 6 + MySQL ( on AWS RDS )
These are the parameters I have configured.
( I'm closing the connection from my Java code also )
<Resource name="jdbc/awsDB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10"
testWhileIdle="true"
maxActive="30"
maxAge="3600"
maxIdle="5"
maxWait="3000"
removeAbandoned="true"
logAbandoned="false"
validationQuery="SELECT 1"
removeAbandonedTimeout="60"
timeBetweenEvictionRunsMillis="15000"
username="sbose78" password="XXXXX"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://xxxxrds.amazonaws.com:3306/health?autoReconnect=true" />
The application works great for the first few database queries and after that the database connection hangs indefinitely.
From the connection logs I've noticed that the connection pooling works good , however, Even when there are 10+ connections in SLEEP state, a new database query takes indefinite time to execute.
I end up re-starting the server in most cases.
What configuration mistake am I doing?
Thanks a lot in advance!
Check whether your query is using all the indexed columns in the where clause.If you are sure about the connection pool, then most probaably the problem will be with query itself.

MVC 3 - After publishing to server - CREATE DATABASE permission denied in database 'master'

Setup
MVC 3 EF 4.1 Code First - Windows 7 Pro 64 Dev machine, Separate Win 7 Pro for host.
I started with a SQLCE4.0 database, and everything worked fine (database initializer created tables, populated, etc...).
Once I got everything working like I wanted, I scripted out my SqlCe DB, created a real SQL 2008 DB, opened the proper ports and enabled TCP connections.
Problem
IF I Change my connection string to point to my intranet server\instance name, with a user I created for this app specifically, it works perfectly running from my dev machine.
IF I publish the app to the server (the web server and sql server are the same), and I try to login to the website (aka access the db), then I get:
CREATE DATABASE permission denied in database 'master'.
I commented out the DBInitializer setup so nothing should be created upon a db hit. The tables are there already.
If I do Database.SetInitializer(null); then I get a different login error:
I don't remember exactly but it was something to the affect of LOGIN FAILED FOR IIS APPPOOL\ApplicationPoolIdentity
So I changed the Identity to NETWORK SERVICE and even tried LOCAL SERVICE, both giving the same login failed error.
I tried following this post who says add a SQL user named 'IIS APPPOOL\MyApplicationName'. SQL Wont let me do that as it has invalid characters ... go figure.
Why does it work perfectly over the network accessing the DB from my dev machine, but when I publish to THE SAME machine that hosts the sql and the site, I get permission denied or login failed errors? I've scoured the net and tried different things.
EDIT: Here are my connection strings:
<add name="AntripContext" connectionString="Data Source=|DataDirectory|AntripWeb.sdf" providerName="System.Data.SqlServerCe.4.0" />
<!--<add name="AntripSQL" connectionString="Data Source=.\sqlexpress;Initial Catalog=Antrip;User Id=myuser;Password=mypassword;" providerName="System.Data.SqlClient" />-->
<add name="AntripWebEntities" connectionString="metadata=res://*/DAL.AntripEntities.csdl|res://*/DAL.AntripEntities.ssdl|res://*/DAL.AntripEntities.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\AntripWeb.sdf"" providerName="System.Data.EntityClient" />
<!--<add name="AntripWebEntities" connectionString="metadata=res://*/DAL.AntripEntities.csdl|res://*/DAL.AntripEntities.ssdl|res://*/DAL.AntripEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\sqlexpress;Initial Catalog=Antrip;User Id=myuser;Password=mypassword;"" providerName="System.Data.EntityClient" />-->
Note: The commented out connection strings are the ones that use the sql 2008 database (that break my published app).
It sounds to me as if your connection string has Integrated Security=true so that you are connecting to SQL as the user you specify, rather than the SQL user you think you are using?
But beware, its still trying to create a database: you need to swap the IDatabaseInitializer - I guess it will look for a DB named the same as the entitys to be careful!