PDA

View Full Version : SQL QThread how i can insert a lock?



patrik08
8th June 2007, 00:28
I have large zip file to insert on a DB (mysql)

How i can insert a lock on a way wo the next Thread not insert data on this table?

or can i solve this if i put the zip packer on the same Thread ... as this

If the user create fast 3 or more page?





/* max bit upload to one page cms zip #define MAXUPLOADFOLDER 2563243 2.44 MB mysql long blob */
class PushQueryZip : public QThread, public Base_Modul
{
Q_OBJECT
public:
void SetupQuery( QSqlDatabase db , uint idmg , const QString filego)
{
LoopNummer = 0;
loopi = true;
dbconnection = db;
having = false;
writteln = filego;
pagenummer = idmg;

}
void run() {

QByteArray inside;
QFile f(writteln);
bool accessf = f.open(QIODevice::ReadOnly);

if (!accessf) {
emit ErrorMsgZip(tr("Unable to open zip."));
exit();
}

inside = f.readAll();
f.close();
f.remove();
const QString blobzip = inside.toBase64()+"|end_stream|";
sqlmake = QString("REPLACE INTO PAGEFILE VALUES (%1, 'zip', 'attach' ,'%2',%3)")
.arg(pagenummer)
.arg(blobzip)
.arg(QTime_Null());

QSqlQuery query("",dbconnection);
bool success = query.exec(sqlmake);
if (!success) {
emit ErrorMsgZip(tr("Unable to connect and exec query..."));
exit();
}
emit OkJobEnd(pagenummer);
exit();
}

signals:
void ErrorMsgZip(QString);
void OkJobEnd(uint);
private:
QSqlDatabase dbconnection;
QString sqlmake;
QString writteln; /* file to go zip ensure dir exist */
QString inside;
int LoopNummer;
uint pagenummer;
bool loopi;
bool having;
};

croftj
8th June 2007, 13:21
You can use a QMutex. Just make is static to your file ei

static QMutext mutex;

Then lock it before calling exec and unlock it once it is done.

If it's early eough in the cycle of the project to change your table, you can make the field a LARGEBLOB and then suck the file into a QByteArray and just insert/update the data like you would an int or a varchar value. This has the advantage of using standard SQL statements and can be used across a couple of differenttypes of databases.

patrik08
8th June 2007, 14:58
You can use a QMutex. Just make is static to your file ei
static QMutext mutex;
Then lock it before calling exec and unlock it once it is done.
If it's early eough in the cycle of the project to change your table, you can make the field a LARGEBLOB and then suck the file into a QByteArray and just insert/update the data like you would an int or a varchar value. This has the advantage of using standard SQL statements and can be used across a couple of differenttypes of databases.

but how run QMutext it create a file ... or only survey process?

my target is to save all open page on this cms whit only one button click.. grab each cache dir (xml.image,attachment,ecc) zip and insert on this QThread...

I observe on send to mysql (same server) need more time as a http PUT Method (3x faster) upload method. the same zip...

MYSQL dont have LARGEBLOB i found only LongBlob and can save 2GB

but i stop the folder on a dirmodel by 2.44 MB ... max mysql packet incomming by server setting.



CREATE TABLE `PAGEFILE` (
`ID` int(32) NOT NULL default '0',
`TIPO` enum('zip','pdf','txt','doc') default NULL,
`TITEL` varchar(255) default NULL,
`PIC` longblob NOT NULL,
`EPOCHE` int(32) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

mm78
12th June 2007, 00:12
This might not be related to your question, but where do you create the QSqlDatabase object that you pass to the SetupQuery() function?

The reason I ask is because it looks like you don't create the connection in the same thread as the one which is using it. This is not safe and may cause problems.

Threads and the SQL Module:
http://doc.trolltech.com/4.3/threads.html#threads-and-the-sql-module

"A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported."