Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - Ok to use TADOConnection in threads

I have created an TCPip server application. The application has one global TADOConnection. This global ado connection is used both for main thread queries and also within threaded processes.

Is this ok? Does the ADOConnection have built in mechanisms to handle multiple queries at the same time?

My application works find in testing environments (2-5 connections). But deployed in a production environment I am getting "unexplainable" access violations at the point the TADOQuery linked to the ADOConnection are set to opened.

Should I be using ADOConnection or should all queries just make the connection to the database on their own (which is probably a bit more resource costly)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each thread needs to have its own connection object. The following link provides more information: http://delphi.about.com/od/kbthread/a/query_threading.htm

Some key points from the article:

1] CoInitialize and CoUninitialize must be called manually before using any of the dbGo objects. Failing to call CoInitialize will result in the "CoInitialize was not called" exception. The CoInitialize method initializes the COM library on the current thread. ADO is COM.

2] You cannot use the TADOConnection object from the main thread (application). Every thread needs to create its own database connection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...