Qt & MySQL first connections fail - mysql

I using Qt 5.15 and I make a standard connection to MySQL
bool open() {
QMutexLocker ml(&fMutex);
fDatabaseNumber = QString::number(++fDatabaseCounter);
fSqlDatabase = QSqlDatabase::addDatabase("QMYSQL", fDatabaseNumber);
fSqlDatabase.setHostName(host);
fSqlDatabase.setDatabaseName(schema);
fSqlDatabase.setUserName(username);
fSqlDatabase.setPassword(password);
if (!fSqlDatabase.open()) {
qDebug() << fSqlDatabase.lastError().databaseText();
return false;
}
fQuery = new QSqlQuery(fSqlDatabase);
return true;
}
Like usually :). I call this code from 10 different threads at same time. And always several of the first connections to database fails with "Can't connect to MySQL server on '127.0.0.1'" message. All of next connections successful, no mater how many connections threads make at the time. I don't know, what I'm doing wrong.

Related

OpenResty / Lua - Maintain mysql database connection in worker threads

I have a simple module called "firewall.lua" that I wrote that has a function
firewall.check_ip(ip) which connects to localhost mysql and performs a query and returns the result. The function gets called from within Location / blocks in nginx sites via access_by_lua_block . The module gets initialized by init_worker_by_lua firewall.init().
Everything works as expected.
What I'd like to do however is maintain the database connection on the worker thread(s) so that I don't have to re-connect every time the function is called but instead re-use the existing connection established by the worker during initialization.
I'm not quite sure how to do this or if its actually doable in openresty/lua. I tried initializing the database connection variables outside of the function to give them scope within the module instead of function and I get various API errors that did not point me in the right direction.
Thank you!
This is possible using the OpenResty cosocket API, which gives you the ability to use a pool of non-blocking connections. There's already one MySQL driver (lua-resty-mysql) which uses the cosocket API. Since you didn't provide a code sample, I'm assuming you're not using it.
Example of a connection and query using lua-resty-mysql (untested):
access_by_lua_block {
local mysql = require "resty.mysql";
local db, err = mysql:new()
db:set_timeout(1000) -- 1 second
local ok, err, errcode, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "my_db",
user = "my_user",
password = "my_pwd",
}
if not ok then
ngx.say("Connection to MySQL failed: ", err)
return
end
result, err, errcode, sqlstate = db:query("select ...")
if not result then
ngx.say("MySQL error: ", err, ".")
return
end
db:close()
}
In case, e.g., you want to control the pool name or use other options, you can pass on additional parameters to connect:
...
local ok, err, errcode, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "my_db",
user = "my_user",
password = "my_pwd",
pool = "my_connection_pool",
}
...
You can find more information in the official docs:
lua-resty-mysql: https://github.com/openresty/lua-resty-mysql
Cosocket API: https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/#ngxsockettcp

MySQL C Connector / mysql_autocommit function

I'm having a little trouble with the mysql connector in C.
I'm converting some (working) node.js scripts to a standalone C application, and for one part of this I need to switch off auto-commit, make two updates to the database and commit.
However, every time I call mysql_autocommit(mysql, 0); it fails - returning non-zero. I know my connection is good, since I've just completed a query and I'm operating on the results of that query.
Interestingly the MySQL logs are empty...which isn't very helpful.
Any ideas how might find out what the problem is, or fix it? The Node.js scripts were also switching off autocommit and [apparently] work.
Thanks in advance
I use this function the following way in my C application:
int main( int argc, char **argv ) {
my_bool reconnect = 1;
...
dbconnect(); // my function (-> conn)
mysql_options( conn, MYSQL_OPT_RECONNECT, &reconnect );
...
exit (0);
}
Then this code switch out the auto-commit:
mysql_autocommit( conn, 0 );
And this switch back:
mysql_commit( conn );
mysql_autocommit( conn, 1 );

Why there are many connections in this case (MySQLNonTransientConnectionException)

I am getting an exception using PreparedStatement to select.
Got an exception accessing TestCase data! null
Problem to connect.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Too many connections
Here is my code:
public Integer getTypeByInputAndProblemId(String inputTestCase, Long problemId) {
String sql = "SELECT type FROM test_case where problem_id= ? and input= ?";
Integer type = 0;
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1, problemId);
ps.setString(2, inputTestCase);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
type = new Integer(rs.getInt("type"));
}
rs.close();
ps.close();
} catch (Exception e) {
System.err.println("Got an exception accessing TestCase data! " + e.getMessage());
}
return type;
}
In line PreparedStatement ps = connection.prepareStatement(sql);
my problem is because connection sometimes is Null (the debug shows this).
I'm guessing this is because of many connections, but I don't know why this is happening.
I would like some help, please!
Yes, the issue happens because your server is reaching the max number of multiple connections accepted by your MySQL Server.
First, you need to see if you have a proper number configured in MySQL for multiple connections: max_connections. If this look low to you, you can increase this number in order to "fix" this issue.
Secondly, if the number makes sense, you're probably using more connections than you think you are. Probably because you're opening connections in your application and not closing them.
Check how many multiple connections your server have used so far.
show status like 'Max_used_connections';
This number is reset when you restart your database service.

REST Web Service to consume MySQL DB

I'm building a REST WebService with JAX-RS and Tomcat to consume a MySQL Database.
I'm following this model:
#Path("/login")
public class Login {
String username;
String password;
// This method is called if POST is requested
#POST
#Produces(MediaType.APPLICATION_XML)
public String loginResponseXML(#FormParam("username") String user, #FormParam("password") String pass) {
//Connection to MySQL Database
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/sakila", "root","larcom");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select first_name, last_name From actor where first_name='" +
user + "' and last_name='" + pass + "'");
while (rs.next()){
System.out.println(rs.getString("first_name") + " " + rs.getString("last_name"));
username = rs.getString("first_name");
password = rs.getString("last_name");
}
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
if (user.equals(username) && pass.equals(password)) {
return ("<?xml version=\"1.0\"?>" + "<auth>200" + "</auth>"); //Success
//return "Success!";
} else {
return ("<?xml version=\"1.0\"?>" + "<auth>404" + "</auth>"); //Damn
//return "Damn!";
}
}
}
I call this method with:
HttpPost httppost = new HttpPost("http://192.168.15.245:8080/org.jersey.andre/rest/login");
Now, my question is:
If I want to query the DB for another table I have to create a new class like Login and make the JDBC connection again?
A new class and a new JDBC connection for each class that make a query to the DB? Performance issues?
Hope you can understand.
Thanks in advance.
A few tips are in order here: Please isolate the DB based code to a "data layer" so to speak...only perform dispatching/business logic within your resource classes.
Now If you are querying a different table, you WILL have a different query! You could either use the same connection (bad) or create a new one and fire a different query(s).
Now whether each resource hits a different table or the same table with a different query depends on your choice of 'representation' for that resource. There is a reason a RDB schema has multiple tables and it's quite common you'll have a different query involving multiple tables or to mutually independent tables.
Performance issues: For 'fresh data' you ARE always going to hit the DB so to speak. If you want to optimize that either develop your own cache (extremely hard) or use approaches like memcached or ehcache to boost performance - before you decide to do that make sure you verify if it's worth it.
Are you going to be having about 1000 DB hits per second? You probably need some performance boosting/handling. Per day...maybe not. Per 2-3 days...YAGNI (You ain't gonna need it, so don't worry for now)
So, for every 'resource' that you design in your application (Login is NOT a resource: See related post: Why is form based authentication NOT considered RESTful?) choose the representation. It may involve different queries etc., for you to return json/xml/xhtml (whatever you choose). Each 'DB related call' should be isolated into it's own 'data layer' - I suggest go with Spring JDBC to make your life easier. It'll take the burden of JDBC plumbing off your shoulders so you can focus on creating your DAOs (Data Access Objects - a patter for Data Access classes. All DAOs logically belong in the data layer)
Hope this helps

Fetching database from MySQL to SQLite

I am trying to complete an iPhone app. And for this i need to fetch a database from MySQL to SQLite. For this case i did use some code like bellow-
MCPConnection *theConnec;
MCPResult *theRes;
//initialize connection string vars
NSString *dbURL = #"XXXXXX";
NSString *userName = #"XXXXXX";
NSString *pass = #"XXXXXX";
//open connection to database
theConnec = [theConnec initToHost:dbURL withLogin:userName password:pass usingPort:3306];
//NSLog(#"The connection to database was successfull");
[theConnec selectDB:#"XXXXXX"];
//{
// NSLog(#"Database found");
//}
//else
//{
// NSLog(#"Database not found");
//}
theRes = [theConnec queryString:#"select * from seahawk_tag"];
//get the number of rows
NSInteger numberOfRows = [theRes numofRows];
NSLog(#"Query of MySQL Database %#", numberOfRows);
return NSApplicationMain(argc, (const char **) argv);
[theConnec release];
But this code does not work properly. Here also mention that i have used some framework like cocoa.framework, cocos2d, openGLES.framework, openAL.framework, APPKit.framework, MCPKit.framework, Quartzcore.framewrok .
and finally i get an error msg that CIColer.h is missing.
freinds, if u know the solution or if u have another code then pls help me
Are you asking how to poll a remote database, or how to pre-export?
Polling a remote database and importing data should be as simple as opening the socket to the remote db, executing the query, and closing again.