Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:18:36+00:00 2026-06-12T17:18:36+00:00

ConnectionFactory factory = new ConnectionFactory {HostName = localhost}; using (IConnection connection = factory.CreateConnection()) using

  • 0
ConnectionFactory factory = new ConnectionFactory {HostName = "localhost"};

using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
    channel.QueueDeclare("hello", false, false, false, null);
    for (int i = 0; i < 100000; i++)
    {
        MemoryStream stream = new MemoryStream();

        var user = new User 
                       {
                           Id = i
                       };

        Serializer.Serialize(stream, user);


        channel.BasicPublish("", "hello", null, stream.ToArray());

    }

}

I have the code above, and I’m curious about thread safety.

I am not sure, but I would imagine ConnectionFactory is thread safe. But is IConnection thread safe? Should I create a connection per request? Or rather a single persistent connection? And what about channel (IModel)?

Also, should I store the connection as ThreadLocal? Or should I create a connection per request?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:18:37+00:00Added an answer on June 12, 2026 at 5:18 pm

    IConnection is thread safe, IModel is not. Generally you should endeavour to keep a connection open for the lifetime of your application. This is especially true if you have consumers which need an open connection in order to receive messages. It’s important to detect and recover from interrupted connections, either because of network or Broker failure. I’d recommend reading ‘RabbitMQ in Action’ by Videla and Williams, especially chapter 6 ‘Writing code that survives failure’.

    Now for a shameless plug. I’m the author of EasyNetQ, a high-level .NET API for RabbitMQ. It does all the connection management for you and will automatically re-connect and rebuild all your subscribers if there’s a network or broker outage. It also provides cluster and fail-over support out of the box. Give it a try.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is the end goal: get a new connection from a factory by name
This is how I create a DataSource with DBCP 1.4 connection factory: PoolableConnectionFactory factory
I'm using rabbitmq java client 2.4.1 the newest version. After a TCP connection lost,
Anyone know if you can create a topic and its connection factory programmatically? Currently
I'm having problems passing some connection info to a runnable thread(using rabbitmq, but I
I am working in a new project.The project consists of using websphere MQ, Websphere
I am using JDBC connection pooling in Tomcat. To retrieve connections I have defined
I have glassfish installed on a server with a JMS ConnectionFactory set up jms/MyConnectionFactory
I am using ActiveMq 5.32 with Spring 2.5.5. I use pretty generic configuration, as
On JBoss 5.1.0 I have Datasource (PostgreSQL 8.3.11) configured using *-ds.xml (standard jboss DS).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.