2

I’m using GridDB Cloud with the official Python client to insert real-time IoT data into a time-series container.
The script runs continuously, but after a few hours I get a connection timeout and inserts stop working.

Code example:

import griddb_python as griddb
from datetime import datetime

factory = griddb.StoreFactory.get_instance()

store = factory.get_store(
    notification_member='xxx.xxx.xxx.xxx:10001',
    cluster_name='myCluster',
    username='admin',
    password='admin'
)

container = store.get_container("sensor_data")

while True:
    row = ["sensor_1", datetime.now(), 22.5]
    container.put(row)  # <-- this fails after a few hours

Error message:

GSConnectionException: Connection timed out

What I want to know:

  1. How can I automatically reconnect to GridDB when this exception happens?
  2. Do I need to call get_store() and get_container() again after a timeout?
  3. Is this the correct approach?
try:
    container.put(row)
except griddb.GSConnectionException:
    # reconnect logic here?

Environment:

  • GridDB Cloud (Free Tier)
  • Python 3.x on Ubuntu 22.04
  • griddb-python client

1 Answer 1

0

So, when a GSConnectionException occurs, the connection to the cluster is lost, so you need to re-establish both the store and container before retrying inserts.

I propose the code below:

import griddb_python as griddb
from datetime import datetime
import time

def connect():
    factory = griddb.StoreFactory.get_instance()
    store = factory.get_store(
        notification_member='xxx.xxx.xxx.xxx:10001',
        cluster_name='myCluster',
        username='admin',
        password='admin'
    )
    container = store.get_container("sensor_data")
    return store, container

store, container = connect()

while True:
    row = ["sensor_1", datetime.now(), 22.5]
    try:
        container.put(row)
    except griddb.GSConnectionException:
        print("Connection lost. Reconnecting...")
        # optional backoff
        time.sleep(5)  
        # re-establish connection
        store, container = connect()  
        container.put(row)  # this is to retry once
    except Exception as e:
        print("Other error:", e)
        time.sleep(1)

Let me know how you get on!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.