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

  • SEARCH
  • Home
  • 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 7494561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:38:17+00:00 2026-05-29T17:38:17+00:00

In mysql we use @ for user-defined variable, which stays alive until the connection

  • 0

In mysql we use @ for user-defined variable, which stays alive until the connection is closed. In java, when multiple threads share one connection pool, while calling a stored procedure concurrently to get rankings:

BEGIN
  SET @rank := 0;
  SELECT @rank := @rank + 1 as rank FROM ...
END

If 2 threads are calling the procedure at same time, without synchronizing @rank, is it possible @rank may return unexpected result?

Is there a better way to handle this situation?

Thanks!

  • 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-05-29T17:38:25+00:00Added an answer on May 29, 2026 at 5:38 pm

    Whether you get “unexpected results” or not depends on what you expect, which is not clear to me from your question.

    Are you trying to isolate the variable to a specific connection, or increment it as a shared counter?

    The scope of a user-defined variable is limited to a single connection, and each connection will be used by a single thread at any given time, so with your example you should expect the value to be isolated within each connection. Since you are re-initializing the value to 0 in your code, you should not see any residual effects of separate processes that used the connection previously.

    However, since you said you are using a stored procedure, I recommend that you declare a local variable inside the stored procedure and use that instead of a user-defined variable.

    The local variable will be scoped to the stored procedure, so you don’t have to worry about the current value affecting a later thread re-using the connection.

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

Sidebar

Related Questions

I'm trying to learn and use SP(Stored Procedure) and UDF(User Defined Function) with MySQL
Is there a way to read a variable at top which is defined some
I would like to pass a value of a user-defined parameter to mysql console
I use MySQL in a fairly complex web site (PHP driven). Ideally, there would
I like to use MySQL to do quantitative analysis and statistics. I would like
I'm about to use MySQL with Hibernate on a Tomcat 5.5.x server. Do I
I am starting to use MySQL with JDBC. Class.forName(com.mysql.jdbc.Driver); conn = DriverManager.getConnection(jdbc:mysql:///x, x, x);
Can I set rails to use mysql with MEMORY as the DB engine? I
I'd like to use MySQL in this form: SELECT 1 AS one, one*2 AS
Is it possible to use MySQL with the data access application block? The existing

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.