Question

Using maxmind/geoip api I have come up with the error "Database has been closed" Anyone have any ideas why this might be?

I have tried multiple different ways of doing this (single line & multiple line solutions etc.) but can't figure out why it isn't working. When debugging I have found that the database is being read for the LookupService line of code as I can see that it has retrieved the country names from the database, but when I try and use

 string userIpAddress = HttpContext.Current.Request.UserHostAddress;
 string geoIpDbPath = "/App_Data/CMSModules/WebAnalytics/MaxMind/";
 string geoIpDb = geoIpDbPath + "GeoIP.dat";
 LookupService ls = new LookupService(geoIpDb, LookupService.GEOIP_MEMORY_CACHE);
 Country c = ls.getCountry(userIpAddress);

This is becoming quite frustrating as I can see that the database has been successfully accessed and the variable 'ls' has been given the appropriate value.

What's wrong with my approach ?

Was it helpful?

Solution

On old version of the api code hides the fact it fails to load the file:

    public LookupService(String databaseFile, int options){
        try {
            this.file = new FileStream(databaseFile, FileMode.Open, FileAccess.Read);
            dboptions = options;
            init();
        } catch(System.SystemException) {
            Console.Write("cannot open file " + databaseFile + "\n");
        }
    }

Each method call then checks if this.file has been set an raises the exception you are seeing

 public Country getCountry(long ipAddress){
            if (file == null) {
                //throw new IllegalStateException("Database has been closed.");
                throw new Exception("Database has been closed.");
            }

OTHER TIPS

It turns out that I made a mistake, and was looking for the file in the wrong location on the server. That said the error messages given by geoip don't make it clear where the error in the code lies and it would be helpful to be told that no .dat file was located and throw the error at the 'LookupService' line rather than the error coming on the 'Country' line. Thanks for those who tried to help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top