# Threads and Accessing MySQL

**URL:** <https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019>\
**Category:** Rails\
**Created:** [March 26, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019 "2008-03-26T19:19:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 26, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/1 "2008-03-26T19:19:45Z")

</div>

Hi,

I have a function which when called triggers a thread. The function  
returns after starting the thread.

Thread should access database update few tables and then should end. But  
I am getting an error when Thread tries to access the database:

Mysql::Error: Lost connection to MySQL server during query: BEGINe[0m

Do i have to open up a new database connection for threads? If so how do  
I do it?

Any suggestions?

Regards,  
Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 26, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/2 "2008-03-26T19:34:39Z")

</div>

And if i don’t invoke this thread, whole app works fine. So MYSQL  
connection is intact in main thread, but on spawned thread hits the  
block with:  
Mysql::Error: Lost connection to MySQL server during query: BEGINe[0m

Sandeep G. wrote:

> Hi,
> 
> I have a function which when called triggers a thread. The function  
> returns after starting the thread.
> 
> Thread should access database update few tables and then should end. But  
> I am getting an error when Thread tries to access the database:
> 
> Mysql::Error: Lost connection to MySQL server during query: BEGINe[0m
> 
> Do i have to open up a new database connection for threads? If so how do  
> I do it?
> 
> Any suggestions?
> 
> Regards,  
> Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 26, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/3 "2008-03-26T20:12:17Z")

</div>

On Mar 26, 6:34 pm, Sandeep G. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> And if i don’t invoke this thread, whole app works fine. So MYSQL  
> connection is intact in main thread, but on spawned thread hits the  
> block with:  
> Mysql::Error: Lost connection to MySQL server during query: BEGIN [0m

Have you set ActiveRecord::Base.allow\_concurency to true ?

Fred

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 26, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/4 "2008-03-26T22:31:06Z")

</div>

AFAIK this is a problem with the mysql connector supplied with Rails.  
Install mysql gem and then use it in the thread to connect to mysql.  
MySQL  
new connections are very light weight and hence there should be no  
problem  
with opening extra connection(I may be wrong here)

On Thu, Mar 27, 2008 at 12:41 AM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 27, 2008, 7:08am UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/5 "2008-03-27T07:08:33Z")

</div>

Frederick C. wrote:

> On Mar 26, 6:34ï¿½pm, Sandeep G. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > And if i don’t invoke this thread, whole app works fine. So MYSQL  
> > connection is intact in main thread, but on spawned thread hits the  
> > block with:  
> > Mysql::Error: Lost connection to MySQL server during query: BEGIN [0m
> 
> Have you set ActiveRecord::Base.allow\_concurency to true ?
> 
> Fred

If I set this in environment.rb, app fails totally ☹ Below is the  
error.  
#\<NoMethodError: undefined method `requires\_reloading?’ for {}:Hash\>

I tried doing , ActiveRecord::Base.establish\_connection with in thread.  
Though it doesn’t fail when i do so, i dont see any actions being  
performed on database though.

I am totally lost! How do I set this allow\_concurrency? My develoment  
machine is windows and I am using webrick.

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 27, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/6 "2008-03-27T13:04:59Z")

</div>

yes you need to do establish\_connection

On Thu, Mar 27, 2008 at 11:40 AM, Sandeep G. \<

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 27, 2008, 7:10am UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/7 "2008-03-27T07:10:17Z")

</div>

Piyush R. wrote:

> AFAIK this is a problem with the mysql connector supplied with Rails.  
> Install mysql gem and then use it in the thread to connect to mysql.  
> MySQL  
> new connections are very light weight and hence there should be no  
> problem  
> with opening extra connection(I may be wrong here)
> 
> On Thu, Mar 27, 2008 at 12:41 AM, Frederick C. \<

Hi Piyush,

I installed mysql-gem (win32) . Do I need to do some intialization in  
the thread before accessing the database?

Regards,  
Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [March 27, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/threads-and-accessing-mysql/133019/8 "2008-03-27T16:44:46Z")

</div>

Is there an easy way to tell if your app is using the mysql connected  
supplied with rails? or the compiled version?

Piyush R. wrote:

> AFAIK this is a problem with the mysql connector supplied with Rails.  
> Install mysql gem and then use it in the thread to connect to mysql.  
> MySQL  
> new connections are very light weight and hence there should be no  
> problem  
> with opening extra connection(I may be wrong here)
> 
> On Thu, Mar 27, 2008 at 12:41 AM, Frederick C. \<
