Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vyacheslav 8 posts 98 karma points
    Jan 28, 2021 @ 13:17
    Vyacheslav
    0

    How to check database connection in U8

    I realized simple healthcheck in my site. I need to know, is connection to database ok or not. For this I want to ping it. How to ping database from api controller code?

  • Huw Reddick 676 posts 2210 karma points
    Jan 28, 2021 @ 13:37
    Huw Reddick
    0

    if your db connection is not correct umbraco will not work, you will get an error trying to access it, so if you are in umbraco then your dbis working.

  • Vyacheslav 8 posts 98 karma points
    Jan 29, 2021 @ 09:19
    Vyacheslav
    0

    It's not always true. When I stop Database and call /umbraco/api/keepalive/ping, it returns

       <Message>I'm alive!</Message>  <Success>true</Success>
    

    But I have to know the state of the database connection. Is there some controller that gives information on the state of the database?

  • Huw Reddick 676 posts 2210 karma points
    Jan 29, 2021 @ 12:56
    Huw Reddick
    0

    a ping will only tell you if the server is up or not, not whether something running on the server is running.

    Did you try turning of the db and then trying to load umbraco? it won't load if the database is not active. So not sure what you are trying to achieve with your check and to what purpose?

    If your database is not live, you won't be able to load umbraco to check the database

  • Vyacheslav 8 posts 98 karma points
    Jan 29, 2021 @ 11:18
    Vyacheslav
    0

    I did this solution

            private int GetDataBaseCode()
            {
                ConnectionState state;
    
                using (var scope = Current.ScopeProvider.CreateScope(autoComplete: true))
                {
                    try
                    {
                        state = scope.Database.Connection.State;
                    }
                    catch
                    {
                        state = ConnectionState.Broken;
                    }
                }  
    
                return state == ConnectionState.Open ? 200 : 500;
            }
    
  • Yakov Lebski 473 posts 1878 karma points
    Jan 29, 2021 @ 19:21
    Yakov Lebski
    101

    I think the best way to use IUmbracoDatabaseFactory

    private readonly IUmbracoDatabaseFactory _umbracoDatabaseFactory;

        public YOURController(IUmbracoDatabaseFactory umbracoDatabaseFactory)
        {
            _umbracoDatabaseFactory = umbracoDatabaseFactory;
        }
    
    _umbracoDatabaseFactory.CanConnect
    
Please Sign in or register to post replies

Write your reply to:

Draft