[{"data":1,"prerenderedAt":3551},["ShallowReactive",2],{"post-python-virtual-environments":3,"rel-python-virtual-environments":322,"sib-python-virtual-environments":3479},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":12,"image":13,"optimized_image":13,"category":14,"tags":15,"author":17,"paginate":6,"body":18,"_type":316,"_id":317,"_source":318,"_file":319,"_stem":320,"_extension":321},"/posts/python-virtual-environments","posts",false,"","Python Virtual Environments (venv)","A Comprehensive Guide to Python Virtual Environments (venv)","2023-09-17 22:00:00","post","A Comprehensive Guide","/assets/img/uploads/python-image.jpg","code",[16,14],"python","jaimedearcos",{"type":19,"children":20,"toc":310},"root",[21,29,36,41,61,67,72,77,230,236,241,270,275,280,299,304],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"text","In the world of Python development, managing dependencies can be a challenging task. You might encounter situations where different projects require different versions of the same library, or you simply want to isolate your project's environment to ensure consistency. That's where Python's venv comes into play. In this tutorial, we'll explore the advantages of using venv in your Python projects, guide you through its installation using pip, and provide an example of how to install dependencies and execute a Python script within your virtual environment.",{"type":22,"tag":30,"props":31,"children":33},"h2",{"id":32},"advantages-of-using-venv",[34],{"type":27,"value":35},"Advantages of Using venv",{"type":22,"tag":23,"props":37,"children":38},{},[39],{"type":27,"value":40},"Python's venv module, introduced in Python 3.3, allows you to create isolated Python environments for your projects. Here are some of the key advantages:",{"type":22,"tag":42,"props":43,"children":44},"ul",{},[45,51,56],{"type":22,"tag":46,"props":47,"children":48},"li",{},[49],{"type":27,"value":50},"Dependency Isolation: venv allows you to create a self-contained environment for each project. This means you can have different versions of Python and project-specific libraries without conflicts.",{"type":22,"tag":46,"props":52,"children":53},{},[54],{"type":27,"value":55},"Version Control Integration: Including your virtual environment in version control (e.g., Git) ensures that others can recreate your development environment precisely, making collaboration smoother.",{"type":22,"tag":46,"props":57,"children":58},{},[59],{"type":27,"value":60},"Easy Cleanup: Virtual environments are easy to create and delete, so you can keep your system clean by removing unused environments when you're done with a project.",{"type":22,"tag":30,"props":62,"children":64},{"id":63},"installing-venv-with-pip",[65],{"type":27,"value":66},"Installing venv with pip",{"type":22,"tag":23,"props":68,"children":69},{},[70],{"type":27,"value":71},"Before you can create virtual environments with venv, make sure you have Python 3.3 or higher installed on your system. Most modern Python installations come with venv included, so you don't need to install it separately.",{"type":22,"tag":23,"props":73,"children":74},{},[75],{"type":27,"value":76},"To create a new virtual environment, follow these steps:",{"type":22,"tag":78,"props":79,"children":80},"ol",{},[81,86,91,140],{"type":22,"tag":46,"props":82,"children":83},{},[84],{"type":27,"value":85},"Open your terminal or command prompt.",{"type":22,"tag":46,"props":87,"children":88},{},[89],{"type":27,"value":90},"Navigate to the directory where you want to create the virtual environment.",{"type":22,"tag":46,"props":92,"children":93},{},[94,96],{"type":27,"value":95},"Run the following command to create a new virtual environment named \"myenv\" (you can replace \"myenv\" with your preferred name):",{"type":22,"tag":97,"props":98,"children":102},"pre",{"className":99,"code":100,"language":101,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","$ python -m venv myenv\n","bash",[103],{"type":22,"tag":14,"props":104,"children":105},{"__ignoreMap":7},[106],{"type":22,"tag":107,"props":108,"children":111},"span",{"class":109,"line":110},"line",1,[112,118,124,130,135],{"type":22,"tag":107,"props":113,"children":115},{"style":114},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[116],{"type":27,"value":117},"$",{"type":22,"tag":107,"props":119,"children":121},{"style":120},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[122],{"type":27,"value":123}," python",{"type":22,"tag":107,"props":125,"children":127},{"style":126},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[128],{"type":27,"value":129}," -m",{"type":22,"tag":107,"props":131,"children":132},{"style":120},[133],{"type":27,"value":134}," venv",{"type":22,"tag":107,"props":136,"children":137},{"style":120},[138],{"type":27,"value":139}," myenv\n",{"type":22,"tag":46,"props":141,"children":142},{},[143,145,153,192,200,224,228],{"type":27,"value":144},"To activate the virtual environment, use the appropriate command for your operating system:",{"type":22,"tag":42,"props":146,"children":147},{},[148],{"type":22,"tag":46,"props":149,"children":150},{},[151],{"type":27,"value":152},"On Windows:",{"type":22,"tag":97,"props":154,"children":156},{"className":99,"code":155,"language":101,"meta":7,"style":7},"$ myenv\\Scripts\\activate\n",[157],{"type":22,"tag":14,"props":158,"children":159},{"__ignoreMap":7},[160],{"type":22,"tag":107,"props":161,"children":162},{"class":109,"line":110},[163,167,172,177,182,187],{"type":22,"tag":107,"props":164,"children":165},{"style":114},[166],{"type":27,"value":117},{"type":22,"tag":107,"props":168,"children":169},{"style":120},[170],{"type":27,"value":171}," myenv",{"type":22,"tag":107,"props":173,"children":174},{"style":126},[175],{"type":27,"value":176},"\\S",{"type":22,"tag":107,"props":178,"children":179},{"style":120},[180],{"type":27,"value":181},"cripts",{"type":22,"tag":107,"props":183,"children":184},{"style":126},[185],{"type":27,"value":186},"\\a",{"type":22,"tag":107,"props":188,"children":189},{"style":120},[190],{"type":27,"value":191},"ctivate\n",{"type":22,"tag":42,"props":193,"children":194},{},[195],{"type":22,"tag":46,"props":196,"children":197},{},[198],{"type":27,"value":199},"On macOS and Linux:",{"type":22,"tag":97,"props":201,"children":203},{"className":99,"code":202,"language":101,"meta":7,"style":7},"$ source myenv/bin/activate\n",[204],{"type":22,"tag":14,"props":205,"children":206},{"__ignoreMap":7},[207],{"type":22,"tag":107,"props":208,"children":209},{"class":109,"line":110},[210,214,219],{"type":22,"tag":107,"props":211,"children":212},{"style":114},[213],{"type":27,"value":117},{"type":22,"tag":107,"props":215,"children":216},{"style":120},[217],{"type":27,"value":218}," source",{"type":22,"tag":107,"props":220,"children":221},{"style":120},[222],{"type":27,"value":223}," myenv/bin/activate\n",{"type":22,"tag":225,"props":226,"children":227},"br",{},[],{"type":27,"value":229},"Once activated, your terminal prompt will change, indicating that you are now working within the virtual environment.",{"type":22,"tag":30,"props":231,"children":233},{"id":232},"installing-dependencies-and-running-a-python-script",[234],{"type":27,"value":235},"Installing Dependencies and Running a Python Script",{"type":22,"tag":23,"props":237,"children":238},{},[239],{"type":27,"value":240},"Now that you're inside your virtual environment, you can install project-specific dependencies using pip. For example, let's install the popular library requests:",{"type":22,"tag":97,"props":242,"children":244},{"className":99,"code":243,"language":101,"meta":7,"style":7},"$ pip install requests\n",[245],{"type":22,"tag":14,"props":246,"children":247},{"__ignoreMap":7},[248],{"type":22,"tag":107,"props":249,"children":250},{"class":109,"line":110},[251,255,260,265],{"type":22,"tag":107,"props":252,"children":253},{"style":114},[254],{"type":27,"value":117},{"type":22,"tag":107,"props":256,"children":257},{"style":120},[258],{"type":27,"value":259}," pip",{"type":22,"tag":107,"props":261,"children":262},{"style":120},[263],{"type":27,"value":264}," install",{"type":22,"tag":107,"props":266,"children":267},{"style":120},[268],{"type":27,"value":269}," requests\n",{"type":22,"tag":23,"props":271,"children":272},{},[273],{"type":27,"value":274},"With your dependencies installed, you can create a Python script or use an existing one within the virtual environment. To run your script, simply execute it as you normally would. All the installed packages will be available for your script to use without affecting the global Python environment.",{"type":22,"tag":23,"props":276,"children":277},{},[278],{"type":27,"value":279},"When you're done working on your project, you can deactivate the virtual environment with the following command:",{"type":22,"tag":97,"props":281,"children":283},{"className":99,"code":282,"language":101,"meta":7,"style":7},"$ deactivate\n",[284],{"type":22,"tag":14,"props":285,"children":286},{"__ignoreMap":7},[287],{"type":22,"tag":107,"props":288,"children":289},{"class":109,"line":110},[290,294],{"type":22,"tag":107,"props":291,"children":292},{"style":114},[293],{"type":27,"value":117},{"type":22,"tag":107,"props":295,"children":296},{"style":120},[297],{"type":27,"value":298}," deactivate\n",{"type":22,"tag":23,"props":300,"children":301},{},[302],{"type":27,"value":303},"And that's it! You've successfully set up and used a virtual environment with venv in Python. This tool is invaluable for managing dependencies and keeping your projects organized. Incorporate venv into your Python development workflow to take full advantage of its benefits.",{"type":22,"tag":305,"props":306,"children":307},"style",{},[308],{"type":27,"value":309},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":311,"depth":311,"links":312},2,[313,314,315],{"id":32,"depth":311,"text":35},{"id":63,"depth":311,"text":66},{"id":232,"depth":311,"text":235},"markdown","content:posts:python-virtual-environments.md","content","posts/python-virtual-environments.md","posts/python-virtual-environments","md",[323,1750,2659],{"_path":324,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":325,"description":326,"date":327,"layout":11,"subtitle":328,"image":329,"optimized_image":329,"category":14,"tags":330,"author":17,"paginate":6,"body":336,"_type":316,"_id":1747,"_source":318,"_file":1748,"_stem":1749,"_extension":321},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png",[14,331,332,333,334,335],"gpt","openai","sql","ai","nlp",{"type":19,"children":337,"toc":1736},[338,347,353,371,377,382,421,434,450,456,461,684,690,695,1031,1037,1042,1393,1399,1404,1463,1469,1474,1655,1661,1679,1685,1690,1697,1732],{"type":22,"tag":339,"props":340,"children":341},"blockquote",{},[342],{"type":22,"tag":23,"props":343,"children":344},{},[345],{"type":27,"value":346},"Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":22,"tag":30,"props":348,"children":350},{"id":349},"requisitos-previos",[351],{"type":27,"value":352},"Requisitos Previos",{"type":22,"tag":42,"props":354,"children":355},{},[356,361,366],{"type":22,"tag":46,"props":357,"children":358},{},[359],{"type":27,"value":360},"Cuenta y clave API de OpenAI",{"type":22,"tag":46,"props":362,"children":363},{},[364],{"type":27,"value":365},"Python instalado en tu entorno",{"type":22,"tag":46,"props":367,"children":368},{},[369],{"type":27,"value":370},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":22,"tag":30,"props":372,"children":374},{"id":373},"paso-1-configuración-del-entorno",[375],{"type":27,"value":376},"Paso 1: Configuración del entorno",{"type":22,"tag":23,"props":378,"children":379},{},[380],{"type":27,"value":381},"Primero, instala las dependencias necesarias:",{"type":22,"tag":97,"props":383,"children":385},{"className":99,"code":384,"language":101,"meta":7,"style":7},"pip install openai python-dotenv sqlalchemy psycopg2-binary\n",[386],{"type":22,"tag":14,"props":387,"children":388},{"__ignoreMap":7},[389],{"type":22,"tag":107,"props":390,"children":391},{"class":109,"line":110},[392,397,401,406,411,416],{"type":22,"tag":107,"props":393,"children":394},{"style":114},[395],{"type":27,"value":396},"pip",{"type":22,"tag":107,"props":398,"children":399},{"style":120},[400],{"type":27,"value":264},{"type":22,"tag":107,"props":402,"children":403},{"style":120},[404],{"type":27,"value":405}," openai",{"type":22,"tag":107,"props":407,"children":408},{"style":120},[409],{"type":27,"value":410}," python-dotenv",{"type":22,"tag":107,"props":412,"children":413},{"style":120},[414],{"type":27,"value":415}," sqlalchemy",{"type":22,"tag":107,"props":417,"children":418},{"style":120},[419],{"type":27,"value":420}," psycopg2-binary\n",{"type":22,"tag":23,"props":422,"children":423},{},[424,426,432],{"type":27,"value":425},"Guarda tu clave API en un archivo ",{"type":22,"tag":14,"props":427,"children":429},{"className":428},[],[430],{"type":27,"value":431},".env",{"type":27,"value":433},":",{"type":22,"tag":97,"props":435,"children":439},{"className":436,"code":437,"language":438,"meta":7,"style":7},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[440],{"type":22,"tag":14,"props":441,"children":442},{"__ignoreMap":7},[443],{"type":22,"tag":107,"props":444,"children":445},{"class":109,"line":110},[446],{"type":22,"tag":107,"props":447,"children":448},{},[449],{"type":27,"value":437},{"type":22,"tag":30,"props":451,"children":453},{"id":452},"paso-2-conexión-a-la-base-de-datos",[454],{"type":27,"value":455},"Paso 2: Conexión a la base de datos",{"type":22,"tag":23,"props":457,"children":458},{},[459],{"type":27,"value":460},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":22,"tag":97,"props":462,"children":465},{"className":463,"code":464,"language":16,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n",[466],{"type":22,"tag":14,"props":467,"children":468},{"__ignoreMap":7},[469,494,515,528,550,560,569,577,606,633,666],{"type":22,"tag":107,"props":470,"children":471},{"class":109,"line":110},[472,478,484,489],{"type":22,"tag":107,"props":473,"children":475},{"style":474},"--shiki-default:#F97583;--shiki-light:#D73A49",[476],{"type":27,"value":477},"from",{"type":22,"tag":107,"props":479,"children":481},{"style":480},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[482],{"type":27,"value":483}," sqlalchemy ",{"type":22,"tag":107,"props":485,"children":486},{"style":474},[487],{"type":27,"value":488},"import",{"type":22,"tag":107,"props":490,"children":491},{"style":480},[492],{"type":27,"value":493}," create_engine\n",{"type":22,"tag":107,"props":495,"children":496},{"class":109,"line":311},[497,501,506,510],{"type":22,"tag":107,"props":498,"children":499},{"style":474},[500],{"type":27,"value":477},{"type":22,"tag":107,"props":502,"children":503},{"style":480},[504],{"type":27,"value":505}," sqlalchemy.orm ",{"type":22,"tag":107,"props":507,"children":508},{"style":474},[509],{"type":27,"value":488},{"type":22,"tag":107,"props":511,"children":512},{"style":480},[513],{"type":27,"value":514}," sessionmaker\n",{"type":22,"tag":107,"props":516,"children":518},{"class":109,"line":517},3,[519,523],{"type":22,"tag":107,"props":520,"children":521},{"style":474},[522],{"type":27,"value":488},{"type":22,"tag":107,"props":524,"children":525},{"style":480},[526],{"type":27,"value":527}," os\n",{"type":22,"tag":107,"props":529,"children":531},{"class":109,"line":530},4,[532,536,541,545],{"type":22,"tag":107,"props":533,"children":534},{"style":474},[535],{"type":27,"value":477},{"type":22,"tag":107,"props":537,"children":538},{"style":480},[539],{"type":27,"value":540}," dotenv ",{"type":22,"tag":107,"props":542,"children":543},{"style":474},[544],{"type":27,"value":488},{"type":22,"tag":107,"props":546,"children":547},{"style":480},[548],{"type":27,"value":549}," load_dotenv\n",{"type":22,"tag":107,"props":551,"children":553},{"class":109,"line":552},5,[554],{"type":22,"tag":107,"props":555,"children":557},{"emptyLinePlaceholder":556},true,[558],{"type":27,"value":559},"\n",{"type":22,"tag":107,"props":561,"children":563},{"class":109,"line":562},6,[564],{"type":22,"tag":107,"props":565,"children":566},{"style":480},[567],{"type":27,"value":568},"load_dotenv()\n",{"type":22,"tag":107,"props":570,"children":572},{"class":109,"line":571},7,[573],{"type":22,"tag":107,"props":574,"children":575},{"emptyLinePlaceholder":556},[576],{"type":27,"value":559},{"type":22,"tag":107,"props":578,"children":580},{"class":109,"line":579},8,[581,586,591,596,601],{"type":22,"tag":107,"props":582,"children":583},{"style":126},[584],{"type":27,"value":585},"DATABASE_URL",{"type":22,"tag":107,"props":587,"children":588},{"style":474},[589],{"type":27,"value":590}," =",{"type":22,"tag":107,"props":592,"children":593},{"style":480},[594],{"type":27,"value":595}," os.getenv(",{"type":22,"tag":107,"props":597,"children":598},{"style":120},[599],{"type":27,"value":600},"\"DATABASE_URL\"",{"type":22,"tag":107,"props":602,"children":603},{"style":480},[604],{"type":27,"value":605},")\n",{"type":22,"tag":107,"props":607,"children":609},{"class":109,"line":608},9,[610,615,620,625,629],{"type":22,"tag":107,"props":611,"children":612},{"style":480},[613],{"type":27,"value":614},"engine ",{"type":22,"tag":107,"props":616,"children":617},{"style":474},[618],{"type":27,"value":619},"=",{"type":22,"tag":107,"props":621,"children":622},{"style":480},[623],{"type":27,"value":624}," create_engine(",{"type":22,"tag":107,"props":626,"children":627},{"style":126},[628],{"type":27,"value":585},{"type":22,"tag":107,"props":630,"children":631},{"style":480},[632],{"type":27,"value":605},{"type":22,"tag":107,"props":634,"children":636},{"class":109,"line":635},10,[637,642,646,651,657,661],{"type":22,"tag":107,"props":638,"children":639},{"style":480},[640],{"type":27,"value":641},"Session ",{"type":22,"tag":107,"props":643,"children":644},{"style":474},[645],{"type":27,"value":619},{"type":22,"tag":107,"props":647,"children":648},{"style":480},[649],{"type":27,"value":650}," sessionmaker(",{"type":22,"tag":107,"props":652,"children":654},{"style":653},"--shiki-default:#FFAB70;--shiki-light:#E36209",[655],{"type":27,"value":656},"bind",{"type":22,"tag":107,"props":658,"children":659},{"style":474},[660],{"type":27,"value":619},{"type":22,"tag":107,"props":662,"children":663},{"style":480},[664],{"type":27,"value":665},"engine)\n",{"type":22,"tag":107,"props":667,"children":669},{"class":109,"line":668},11,[670,675,679],{"type":22,"tag":107,"props":671,"children":672},{"style":480},[673],{"type":27,"value":674},"session ",{"type":22,"tag":107,"props":676,"children":677},{"style":474},[678],{"type":27,"value":619},{"type":22,"tag":107,"props":680,"children":681},{"style":480},[682],{"type":27,"value":683}," Session()\n",{"type":22,"tag":30,"props":685,"children":687},{"id":686},"paso-3-obtener-esquema-de-la-base-de-datos",[688],{"type":27,"value":689},"Paso 3: Obtener esquema de la base de datos",{"type":22,"tag":23,"props":691,"children":692},{},[693],{"type":27,"value":694},"Genera una descripción del esquema que será utilizada por GPT:",{"type":22,"tag":97,"props":696,"children":698},{"className":463,"code":697,"language":16,"meta":7,"style":7},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[699],{"type":22,"tag":14,"props":700,"children":701},{"__ignoreMap":7},[702,722,729,747,764,781,804,847,869,954,978,991,999,1017],{"type":22,"tag":107,"props":703,"children":704},{"class":109,"line":110},[705,709,713,717],{"type":22,"tag":107,"props":706,"children":707},{"style":474},[708],{"type":27,"value":477},{"type":22,"tag":107,"props":710,"children":711},{"style":480},[712],{"type":27,"value":483},{"type":22,"tag":107,"props":714,"children":715},{"style":474},[716],{"type":27,"value":488},{"type":22,"tag":107,"props":718,"children":719},{"style":480},[720],{"type":27,"value":721}," inspect\n",{"type":22,"tag":107,"props":723,"children":724},{"class":109,"line":311},[725],{"type":22,"tag":107,"props":726,"children":727},{"emptyLinePlaceholder":556},[728],{"type":27,"value":559},{"type":22,"tag":107,"props":730,"children":731},{"class":109,"line":517},[732,737,742],{"type":22,"tag":107,"props":733,"children":734},{"style":474},[735],{"type":27,"value":736},"def",{"type":22,"tag":107,"props":738,"children":739},{"style":114},[740],{"type":27,"value":741}," get_database_schema",{"type":22,"tag":107,"props":743,"children":744},{"style":480},[745],{"type":27,"value":746},"(engine):\n",{"type":22,"tag":107,"props":748,"children":749},{"class":109,"line":530},[750,755,759],{"type":22,"tag":107,"props":751,"children":752},{"style":480},[753],{"type":27,"value":754},"    inspector ",{"type":22,"tag":107,"props":756,"children":757},{"style":474},[758],{"type":27,"value":619},{"type":22,"tag":107,"props":760,"children":761},{"style":480},[762],{"type":27,"value":763}," inspect(engine)\n",{"type":22,"tag":107,"props":765,"children":766},{"class":109,"line":552},[767,772,776],{"type":22,"tag":107,"props":768,"children":769},{"style":480},[770],{"type":27,"value":771},"    schema_description ",{"type":22,"tag":107,"props":773,"children":774},{"style":474},[775],{"type":27,"value":619},{"type":22,"tag":107,"props":777,"children":778},{"style":120},[779],{"type":27,"value":780}," \"\"\n",{"type":22,"tag":107,"props":782,"children":783},{"class":109,"line":562},[784,789,794,799],{"type":22,"tag":107,"props":785,"children":786},{"style":474},[787],{"type":27,"value":788},"    for",{"type":22,"tag":107,"props":790,"children":791},{"style":480},[792],{"type":27,"value":793}," table_name ",{"type":22,"tag":107,"props":795,"children":796},{"style":474},[797],{"type":27,"value":798},"in",{"type":22,"tag":107,"props":800,"children":801},{"style":480},[802],{"type":27,"value":803}," inspector.get_table_names():\n",{"type":22,"tag":107,"props":805,"children":806},{"class":109,"line":571},[807,812,817,822,827,832,837,842],{"type":22,"tag":107,"props":808,"children":809},{"style":480},[810],{"type":27,"value":811},"        schema_description ",{"type":22,"tag":107,"props":813,"children":814},{"style":474},[815],{"type":27,"value":816},"+=",{"type":22,"tag":107,"props":818,"children":819},{"style":474},[820],{"type":27,"value":821}," f",{"type":22,"tag":107,"props":823,"children":824},{"style":120},[825],{"type":27,"value":826},"\"Table: ",{"type":22,"tag":107,"props":828,"children":829},{"style":126},[830],{"type":27,"value":831},"{",{"type":22,"tag":107,"props":833,"children":834},{"style":480},[835],{"type":27,"value":836},"table_name",{"type":22,"tag":107,"props":838,"children":839},{"style":126},[840],{"type":27,"value":841},"}\\n",{"type":22,"tag":107,"props":843,"children":844},{"style":120},[845],{"type":27,"value":846},"\"\n",{"type":22,"tag":107,"props":848,"children":849},{"class":109,"line":579},[850,855,860,864],{"type":22,"tag":107,"props":851,"children":852},{"style":474},[853],{"type":27,"value":854},"        for",{"type":22,"tag":107,"props":856,"children":857},{"style":480},[858],{"type":27,"value":859}," column ",{"type":22,"tag":107,"props":861,"children":862},{"style":474},[863],{"type":27,"value":798},{"type":22,"tag":107,"props":865,"children":866},{"style":480},[867],{"type":27,"value":868}," inspector.get_columns(table_name):\n",{"type":22,"tag":107,"props":870,"children":871},{"class":109,"line":608},[872,877,881,885,890,894,899,904,909,914,919,923,927,932,936,940,945,950],{"type":22,"tag":107,"props":873,"children":874},{"style":480},[875],{"type":27,"value":876},"            schema_description ",{"type":22,"tag":107,"props":878,"children":879},{"style":474},[880],{"type":27,"value":816},{"type":22,"tag":107,"props":882,"children":883},{"style":474},[884],{"type":27,"value":821},{"type":22,"tag":107,"props":886,"children":887},{"style":120},[888],{"type":27,"value":889},"\"- ",{"type":22,"tag":107,"props":891,"children":892},{"style":126},[893],{"type":27,"value":831},{"type":22,"tag":107,"props":895,"children":896},{"style":480},[897],{"type":27,"value":898},"column[",{"type":22,"tag":107,"props":900,"children":901},{"style":120},[902],{"type":27,"value":903},"'name'",{"type":22,"tag":107,"props":905,"children":906},{"style":480},[907],{"type":27,"value":908},"]",{"type":22,"tag":107,"props":910,"children":911},{"style":126},[912],{"type":27,"value":913},"}",{"type":22,"tag":107,"props":915,"children":916},{"style":120},[917],{"type":27,"value":918}," (",{"type":22,"tag":107,"props":920,"children":921},{"style":126},[922],{"type":27,"value":831},{"type":22,"tag":107,"props":924,"children":925},{"style":480},[926],{"type":27,"value":898},{"type":22,"tag":107,"props":928,"children":929},{"style":120},[930],{"type":27,"value":931},"'type'",{"type":22,"tag":107,"props":933,"children":934},{"style":480},[935],{"type":27,"value":908},{"type":22,"tag":107,"props":937,"children":938},{"style":126},[939],{"type":27,"value":913},{"type":22,"tag":107,"props":941,"children":942},{"style":120},[943],{"type":27,"value":944},")",{"type":22,"tag":107,"props":946,"children":947},{"style":126},[948],{"type":27,"value":949},"\\n",{"type":22,"tag":107,"props":951,"children":952},{"style":120},[953],{"type":27,"value":846},{"type":22,"tag":107,"props":955,"children":956},{"class":109,"line":635},[957,961,965,970,974],{"type":22,"tag":107,"props":958,"children":959},{"style":480},[960],{"type":27,"value":811},{"type":22,"tag":107,"props":962,"children":963},{"style":474},[964],{"type":27,"value":816},{"type":22,"tag":107,"props":966,"children":967},{"style":120},[968],{"type":27,"value":969}," \"",{"type":22,"tag":107,"props":971,"children":972},{"style":126},[973],{"type":27,"value":949},{"type":22,"tag":107,"props":975,"children":976},{"style":120},[977],{"type":27,"value":846},{"type":22,"tag":107,"props":979,"children":980},{"class":109,"line":668},[981,986],{"type":22,"tag":107,"props":982,"children":983},{"style":474},[984],{"type":27,"value":985},"    return",{"type":22,"tag":107,"props":987,"children":988},{"style":480},[989],{"type":27,"value":990}," schema_description\n",{"type":22,"tag":107,"props":992,"children":994},{"class":109,"line":993},12,[995],{"type":22,"tag":107,"props":996,"children":997},{"emptyLinePlaceholder":556},[998],{"type":27,"value":559},{"type":22,"tag":107,"props":1000,"children":1002},{"class":109,"line":1001},13,[1003,1008,1012],{"type":22,"tag":107,"props":1004,"children":1005},{"style":480},[1006],{"type":27,"value":1007},"schema_description ",{"type":22,"tag":107,"props":1009,"children":1010},{"style":474},[1011],{"type":27,"value":619},{"type":22,"tag":107,"props":1013,"children":1014},{"style":480},[1015],{"type":27,"value":1016}," get_database_schema(engine)\n",{"type":22,"tag":107,"props":1018,"children":1020},{"class":109,"line":1019},14,[1021,1026],{"type":22,"tag":107,"props":1022,"children":1023},{"style":126},[1024],{"type":27,"value":1025},"print",{"type":22,"tag":107,"props":1027,"children":1028},{"style":480},[1029],{"type":27,"value":1030},"(schema_description)\n",{"type":22,"tag":30,"props":1032,"children":1034},{"id":1033},"paso-4-preparar-la-consulta-con-gpt",[1035],{"type":27,"value":1036},"Paso 4: Preparar la consulta con GPT",{"type":22,"tag":23,"props":1038,"children":1039},{},[1040],{"type":27,"value":1041},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":22,"tag":97,"props":1043,"children":1045},{"className":463,"code":1044,"language":16,"meta":7,"style":7},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[1046],{"type":22,"tag":14,"props":1047,"children":1048},{"__ignoreMap":7},[1049,1061,1072,1079,1104,1111,1128,1145,1167,1184,1255,1288,1296,1304,1331,1344,1352,1380],{"type":22,"tag":107,"props":1050,"children":1051},{"class":109,"line":110},[1052,1056],{"type":22,"tag":107,"props":1053,"children":1054},{"style":474},[1055],{"type":27,"value":488},{"type":22,"tag":107,"props":1057,"children":1058},{"style":480},[1059],{"type":27,"value":1060}," openai\n",{"type":22,"tag":107,"props":1062,"children":1063},{"class":109,"line":311},[1064,1068],{"type":22,"tag":107,"props":1065,"children":1066},{"style":474},[1067],{"type":27,"value":488},{"type":22,"tag":107,"props":1069,"children":1070},{"style":480},[1071],{"type":27,"value":527},{"type":22,"tag":107,"props":1073,"children":1074},{"class":109,"line":517},[1075],{"type":22,"tag":107,"props":1076,"children":1077},{"emptyLinePlaceholder":556},[1078],{"type":27,"value":559},{"type":22,"tag":107,"props":1080,"children":1081},{"class":109,"line":530},[1082,1087,1091,1095,1100],{"type":22,"tag":107,"props":1083,"children":1084},{"style":480},[1085],{"type":27,"value":1086},"openai.api_key ",{"type":22,"tag":107,"props":1088,"children":1089},{"style":474},[1090],{"type":27,"value":619},{"type":22,"tag":107,"props":1092,"children":1093},{"style":480},[1094],{"type":27,"value":595},{"type":22,"tag":107,"props":1096,"children":1097},{"style":120},[1098],{"type":27,"value":1099},"\"OPENAI_API_KEY\"",{"type":22,"tag":107,"props":1101,"children":1102},{"style":480},[1103],{"type":27,"value":605},{"type":22,"tag":107,"props":1105,"children":1106},{"class":109,"line":552},[1107],{"type":22,"tag":107,"props":1108,"children":1109},{"emptyLinePlaceholder":556},[1110],{"type":27,"value":559},{"type":22,"tag":107,"props":1112,"children":1113},{"class":109,"line":562},[1114,1118,1123],{"type":22,"tag":107,"props":1115,"children":1116},{"style":474},[1117],{"type":27,"value":736},{"type":22,"tag":107,"props":1119,"children":1120},{"style":114},[1121],{"type":27,"value":1122}," generate_sql",{"type":22,"tag":107,"props":1124,"children":1125},{"style":480},[1126],{"type":27,"value":1127},"(question, schema):\n",{"type":22,"tag":107,"props":1129,"children":1130},{"class":109,"line":571},[1131,1136,1140],{"type":22,"tag":107,"props":1132,"children":1133},{"style":480},[1134],{"type":27,"value":1135},"    response ",{"type":22,"tag":107,"props":1137,"children":1138},{"style":474},[1139],{"type":27,"value":619},{"type":22,"tag":107,"props":1141,"children":1142},{"style":480},[1143],{"type":27,"value":1144}," openai.ChatCompletion.create(\n",{"type":22,"tag":107,"props":1146,"children":1147},{"class":109,"line":579},[1148,1153,1157,1162],{"type":22,"tag":107,"props":1149,"children":1150},{"style":653},[1151],{"type":27,"value":1152},"        model",{"type":22,"tag":107,"props":1154,"children":1155},{"style":474},[1156],{"type":27,"value":619},{"type":22,"tag":107,"props":1158,"children":1159},{"style":120},[1160],{"type":27,"value":1161},"\"gpt-4\"",{"type":22,"tag":107,"props":1163,"children":1164},{"style":480},[1165],{"type":27,"value":1166},",\n",{"type":22,"tag":107,"props":1168,"children":1169},{"class":109,"line":608},[1170,1175,1179],{"type":22,"tag":107,"props":1171,"children":1172},{"style":653},[1173],{"type":27,"value":1174},"        messages",{"type":22,"tag":107,"props":1176,"children":1177},{"style":474},[1178],{"type":27,"value":619},{"type":22,"tag":107,"props":1180,"children":1181},{"style":480},[1182],{"type":27,"value":1183},"[\n",{"type":22,"tag":107,"props":1185,"children":1186},{"class":109,"line":635},[1187,1192,1197,1202,1207,1212,1217,1221,1226,1231,1236,1241,1245,1250],{"type":22,"tag":107,"props":1188,"children":1189},{"style":480},[1190],{"type":27,"value":1191},"            {",{"type":22,"tag":107,"props":1193,"children":1194},{"style":120},[1195],{"type":27,"value":1196},"\"role\"",{"type":22,"tag":107,"props":1198,"children":1199},{"style":480},[1200],{"type":27,"value":1201},": ",{"type":22,"tag":107,"props":1203,"children":1204},{"style":120},[1205],{"type":27,"value":1206},"\"system\"",{"type":22,"tag":107,"props":1208,"children":1209},{"style":480},[1210],{"type":27,"value":1211},", ",{"type":22,"tag":107,"props":1213,"children":1214},{"style":120},[1215],{"type":27,"value":1216},"\"content\"",{"type":22,"tag":107,"props":1218,"children":1219},{"style":480},[1220],{"type":27,"value":1201},{"type":22,"tag":107,"props":1222,"children":1223},{"style":474},[1224],{"type":27,"value":1225},"f",{"type":22,"tag":107,"props":1227,"children":1228},{"style":120},[1229],{"type":27,"value":1230},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":22,"tag":107,"props":1232,"children":1233},{"style":126},[1234],{"type":27,"value":1235},"\\n{",{"type":22,"tag":107,"props":1237,"children":1238},{"style":480},[1239],{"type":27,"value":1240},"schema",{"type":22,"tag":107,"props":1242,"children":1243},{"style":126},[1244],{"type":27,"value":913},{"type":22,"tag":107,"props":1246,"children":1247},{"style":120},[1248],{"type":27,"value":1249},"\"",{"type":22,"tag":107,"props":1251,"children":1252},{"style":480},[1253],{"type":27,"value":1254},"},\n",{"type":22,"tag":107,"props":1256,"children":1257},{"class":109,"line":668},[1258,1262,1266,1270,1275,1279,1283],{"type":22,"tag":107,"props":1259,"children":1260},{"style":480},[1261],{"type":27,"value":1191},{"type":22,"tag":107,"props":1263,"children":1264},{"style":120},[1265],{"type":27,"value":1196},{"type":22,"tag":107,"props":1267,"children":1268},{"style":480},[1269],{"type":27,"value":1201},{"type":22,"tag":107,"props":1271,"children":1272},{"style":120},[1273],{"type":27,"value":1274},"\"user\"",{"type":22,"tag":107,"props":1276,"children":1277},{"style":480},[1278],{"type":27,"value":1211},{"type":22,"tag":107,"props":1280,"children":1281},{"style":120},[1282],{"type":27,"value":1216},{"type":22,"tag":107,"props":1284,"children":1285},{"style":480},[1286],{"type":27,"value":1287},": question}\n",{"type":22,"tag":107,"props":1289,"children":1290},{"class":109,"line":993},[1291],{"type":22,"tag":107,"props":1292,"children":1293},{"style":480},[1294],{"type":27,"value":1295},"        ]\n",{"type":22,"tag":107,"props":1297,"children":1298},{"class":109,"line":1001},[1299],{"type":22,"tag":107,"props":1300,"children":1301},{"style":480},[1302],{"type":27,"value":1303},"    )\n",{"type":22,"tag":107,"props":1305,"children":1306},{"class":109,"line":1019},[1307,1312,1316,1321,1326],{"type":22,"tag":107,"props":1308,"children":1309},{"style":480},[1310],{"type":27,"value":1311},"    sql_query ",{"type":22,"tag":107,"props":1313,"children":1314},{"style":474},[1315],{"type":27,"value":619},{"type":22,"tag":107,"props":1317,"children":1318},{"style":480},[1319],{"type":27,"value":1320}," response.choices[",{"type":22,"tag":107,"props":1322,"children":1323},{"style":126},[1324],{"type":27,"value":1325},"0",{"type":22,"tag":107,"props":1327,"children":1328},{"style":480},[1329],{"type":27,"value":1330},"].message.content.strip()\n",{"type":22,"tag":107,"props":1332,"children":1334},{"class":109,"line":1333},15,[1335,1339],{"type":22,"tag":107,"props":1336,"children":1337},{"style":474},[1338],{"type":27,"value":985},{"type":22,"tag":107,"props":1340,"children":1341},{"style":480},[1342],{"type":27,"value":1343}," sql_query\n",{"type":22,"tag":107,"props":1345,"children":1347},{"class":109,"line":1346},16,[1348],{"type":22,"tag":107,"props":1349,"children":1350},{"emptyLinePlaceholder":556},[1351],{"type":27,"value":559},{"type":22,"tag":107,"props":1353,"children":1355},{"class":109,"line":1354},17,[1356,1361,1365,1370,1375],{"type":22,"tag":107,"props":1357,"children":1358},{"style":480},[1359],{"type":27,"value":1360},"query ",{"type":22,"tag":107,"props":1362,"children":1363},{"style":474},[1364],{"type":27,"value":619},{"type":22,"tag":107,"props":1366,"children":1367},{"style":480},[1368],{"type":27,"value":1369}," generate_sql(",{"type":22,"tag":107,"props":1371,"children":1372},{"style":120},[1373],{"type":27,"value":1374},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":22,"tag":107,"props":1376,"children":1377},{"style":480},[1378],{"type":27,"value":1379},", schema_description)\n",{"type":22,"tag":107,"props":1381,"children":1383},{"class":109,"line":1382},18,[1384,1388],{"type":22,"tag":107,"props":1385,"children":1386},{"style":126},[1387],{"type":27,"value":1025},{"type":22,"tag":107,"props":1389,"children":1390},{"style":480},[1391],{"type":27,"value":1392},"(query)\n",{"type":22,"tag":30,"props":1394,"children":1396},{"id":1395},"paso-5-ejecutar-consultas-generadas",[1397],{"type":27,"value":1398},"Paso 5: Ejecutar consultas generadas",{"type":22,"tag":23,"props":1400,"children":1401},{},[1402],{"type":27,"value":1403},"Ejecuta la consulta generada y obtiene resultados:",{"type":22,"tag":97,"props":1405,"children":1407},{"className":463,"code":1406,"language":16,"meta":7,"style":7},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[1408],{"type":22,"tag":14,"props":1409,"children":1410},{"__ignoreMap":7},[1411,1428,1450],{"type":22,"tag":107,"props":1412,"children":1413},{"class":109,"line":110},[1414,1419,1423],{"type":22,"tag":107,"props":1415,"children":1416},{"style":480},[1417],{"type":27,"value":1418},"result ",{"type":22,"tag":107,"props":1420,"children":1421},{"style":474},[1422],{"type":27,"value":619},{"type":22,"tag":107,"props":1424,"children":1425},{"style":480},[1426],{"type":27,"value":1427}," session.execute(query)\n",{"type":22,"tag":107,"props":1429,"children":1430},{"class":109,"line":311},[1431,1436,1441,1445],{"type":22,"tag":107,"props":1432,"children":1433},{"style":474},[1434],{"type":27,"value":1435},"for",{"type":22,"tag":107,"props":1437,"children":1438},{"style":480},[1439],{"type":27,"value":1440}," row ",{"type":22,"tag":107,"props":1442,"children":1443},{"style":474},[1444],{"type":27,"value":798},{"type":22,"tag":107,"props":1446,"children":1447},{"style":480},[1448],{"type":27,"value":1449}," result:\n",{"type":22,"tag":107,"props":1451,"children":1452},{"class":109,"line":517},[1453,1458],{"type":22,"tag":107,"props":1454,"children":1455},{"style":126},[1456],{"type":27,"value":1457},"    print",{"type":22,"tag":107,"props":1459,"children":1460},{"style":480},[1461],{"type":27,"value":1462},"(row)\n",{"type":22,"tag":30,"props":1464,"children":1466},{"id":1465},"paso-6-manejo-de-errores-y-seguridad",[1467],{"type":27,"value":1468},"Paso 6: Manejo de errores y seguridad",{"type":22,"tag":23,"props":1470,"children":1471},{},[1472],{"type":27,"value":1473},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":22,"tag":97,"props":1475,"children":1477},{"className":463,"code":1476,"language":16,"meta":7,"style":7},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[1478],{"type":22,"tag":14,"props":1479,"children":1480},{"__ignoreMap":7},[1481,1498,1511,1528,1541,1564,1607,1619,1626,1643],{"type":22,"tag":107,"props":1482,"children":1483},{"class":109,"line":110},[1484,1488,1493],{"type":22,"tag":107,"props":1485,"children":1486},{"style":474},[1487],{"type":27,"value":736},{"type":22,"tag":107,"props":1489,"children":1490},{"style":114},[1491],{"type":27,"value":1492}," execute_safe_query",{"type":22,"tag":107,"props":1494,"children":1495},{"style":480},[1496],{"type":27,"value":1497},"(sql_query):\n",{"type":22,"tag":107,"props":1499,"children":1500},{"class":109,"line":311},[1501,1506],{"type":22,"tag":107,"props":1502,"children":1503},{"style":474},[1504],{"type":27,"value":1505},"    try",{"type":22,"tag":107,"props":1507,"children":1508},{"style":480},[1509],{"type":27,"value":1510},":\n",{"type":22,"tag":107,"props":1512,"children":1513},{"class":109,"line":517},[1514,1519,1523],{"type":22,"tag":107,"props":1515,"children":1516},{"style":480},[1517],{"type":27,"value":1518},"        result ",{"type":22,"tag":107,"props":1520,"children":1521},{"style":474},[1522],{"type":27,"value":619},{"type":22,"tag":107,"props":1524,"children":1525},{"style":480},[1526],{"type":27,"value":1527}," session.execute(sql_query)\n",{"type":22,"tag":107,"props":1529,"children":1530},{"class":109,"line":530},[1531,1536],{"type":22,"tag":107,"props":1532,"children":1533},{"style":474},[1534],{"type":27,"value":1535},"        return",{"type":22,"tag":107,"props":1537,"children":1538},{"style":480},[1539],{"type":27,"value":1540}," result.fetchall()\n",{"type":22,"tag":107,"props":1542,"children":1543},{"class":109,"line":552},[1544,1549,1554,1559],{"type":22,"tag":107,"props":1545,"children":1546},{"style":474},[1547],{"type":27,"value":1548},"    except",{"type":22,"tag":107,"props":1550,"children":1551},{"style":126},[1552],{"type":27,"value":1553}," Exception",{"type":22,"tag":107,"props":1555,"children":1556},{"style":474},[1557],{"type":27,"value":1558}," as",{"type":22,"tag":107,"props":1560,"children":1561},{"style":480},[1562],{"type":27,"value":1563}," e:\n",{"type":22,"tag":107,"props":1565,"children":1566},{"class":109,"line":562},[1567,1572,1577,1581,1586,1590,1595,1599,1603],{"type":22,"tag":107,"props":1568,"children":1569},{"style":126},[1570],{"type":27,"value":1571},"        print",{"type":22,"tag":107,"props":1573,"children":1574},{"style":480},[1575],{"type":27,"value":1576},"(",{"type":22,"tag":107,"props":1578,"children":1579},{"style":474},[1580],{"type":27,"value":1225},{"type":22,"tag":107,"props":1582,"children":1583},{"style":120},[1584],{"type":27,"value":1585},"\"Error al ejecutar consulta: ",{"type":22,"tag":107,"props":1587,"children":1588},{"style":126},[1589],{"type":27,"value":831},{"type":22,"tag":107,"props":1591,"children":1592},{"style":480},[1593],{"type":27,"value":1594},"e",{"type":22,"tag":107,"props":1596,"children":1597},{"style":126},[1598],{"type":27,"value":913},{"type":22,"tag":107,"props":1600,"children":1601},{"style":120},[1602],{"type":27,"value":1249},{"type":22,"tag":107,"props":1604,"children":1605},{"style":480},[1606],{"type":27,"value":605},{"type":22,"tag":107,"props":1608,"children":1609},{"class":109,"line":571},[1610,1614],{"type":22,"tag":107,"props":1611,"children":1612},{"style":474},[1613],{"type":27,"value":1535},{"type":22,"tag":107,"props":1615,"children":1616},{"style":126},[1617],{"type":27,"value":1618}," None\n",{"type":22,"tag":107,"props":1620,"children":1621},{"class":109,"line":579},[1622],{"type":22,"tag":107,"props":1623,"children":1624},{"emptyLinePlaceholder":556},[1625],{"type":27,"value":559},{"type":22,"tag":107,"props":1627,"children":1628},{"class":109,"line":608},[1629,1634,1638],{"type":22,"tag":107,"props":1630,"children":1631},{"style":480},[1632],{"type":27,"value":1633},"results ",{"type":22,"tag":107,"props":1635,"children":1636},{"style":474},[1637],{"type":27,"value":619},{"type":22,"tag":107,"props":1639,"children":1640},{"style":480},[1641],{"type":27,"value":1642}," execute_safe_query(query)\n",{"type":22,"tag":107,"props":1644,"children":1645},{"class":109,"line":635},[1646,1650],{"type":22,"tag":107,"props":1647,"children":1648},{"style":126},[1649],{"type":27,"value":1025},{"type":22,"tag":107,"props":1651,"children":1652},{"style":480},[1653],{"type":27,"value":1654},"(results)\n",{"type":22,"tag":30,"props":1656,"children":1658},{"id":1657},"buenas-prácticas",[1659],{"type":27,"value":1660},"Buenas prácticas",{"type":22,"tag":42,"props":1662,"children":1663},{},[1664,1669,1674],{"type":22,"tag":46,"props":1665,"children":1666},{},[1667],{"type":27,"value":1668},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":22,"tag":46,"props":1670,"children":1671},{},[1672],{"type":27,"value":1673},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":22,"tag":46,"props":1675,"children":1676},{},[1677],{"type":27,"value":1678},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":22,"tag":30,"props":1680,"children":1682},{"id":1681},"conclusión",[1683],{"type":27,"value":1684},"Conclusión",{"type":22,"tag":23,"props":1686,"children":1687},{},[1688],{"type":27,"value":1689},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":22,"tag":1691,"props":1692,"children":1694},"h1",{"id":1693},"recursos-adicionales",[1695],{"type":27,"value":1696},"Recursos adicionales",{"type":22,"tag":42,"props":1698,"children":1699},{},[1700,1712,1722],{"type":22,"tag":46,"props":1701,"children":1702},{},[1703],{"type":22,"tag":1704,"props":1705,"children":1709},"a",{"href":1706,"rel":1707},"https://platform.openai.com/docs/api-reference",[1708],"nofollow",[1710],{"type":27,"value":1711},"Documentación API OpenAI",{"type":22,"tag":46,"props":1713,"children":1714},{},[1715],{"type":22,"tag":1704,"props":1716,"children":1719},{"href":1717,"rel":1718},"https://docs.sqlalchemy.org/en/20/",[1708],[1720],{"type":27,"value":1721},"SQLAlchemy ORM Documentation",{"type":22,"tag":46,"props":1723,"children":1724},{},[1725],{"type":22,"tag":1704,"props":1726,"children":1729},{"href":1727,"rel":1728},"https://pypi.org/project/python-dotenv/",[1708],[1730],{"type":27,"value":1731},"Python Dotenv",{"type":22,"tag":305,"props":1733,"children":1734},{},[1735],{"type":27,"value":309},{"title":7,"searchDepth":311,"depth":311,"links":1737},[1738,1739,1740,1741,1742,1743,1744,1745,1746],{"id":349,"depth":311,"text":352},{"id":373,"depth":311,"text":376},{"id":452,"depth":311,"text":455},{"id":686,"depth":311,"text":689},{"id":1033,"depth":311,"text":1036},{"id":1395,"depth":311,"text":1398},{"id":1465,"depth":311,"text":1468},{"id":1657,"depth":311,"text":1660},{"id":1681,"depth":311,"text":1684},"content:posts:gpt-sql-integration.md","posts/gpt-sql-integration.md","posts/gpt-sql-integration",{"_path":1751,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":1752,"description":1753,"date":1754,"layout":11,"subtitle":1755,"image":1756,"optimized_image":1756,"category":14,"tags":1757,"author":17,"paginate":6,"body":1762,"_type":316,"_id":2656,"_source":318,"_file":2657,"_stem":2658,"_extension":321},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","Explore essential security strategies for Java applications, addressing common vulnerabilities identified by OWASP Top 10 with practical examples.","2024-08-15T11:00:00.000Z","Practical Strategies to Safeguard Your Java Applications","/assets/img/uploads/java-security-best-practices.png",[14,1758,1759,1760,1761],"java","security","owasp","cybersecurity",{"type":19,"children":1763,"toc":2644},[1764,1772,1778,1783,1836,1841,1847,1852,1975,1980,1986,1991,2094,2100,2105,2228,2233,2239,2244,2269,2274,2280,2285,2301,2306,2312,2317,2400,2405,2411,2416,2536,2541,2547,2552,2585,2590,2596,2601,2607,2640],{"type":22,"tag":339,"props":1765,"children":1766},{},[1767],{"type":22,"tag":23,"props":1768,"children":1769},{},[1770],{"type":27,"value":1771},"Security is critical in software development. Java applications, like all software, face numerous potential vulnerabilities. In this article, we will explore essential security best practices, referencing the OWASP Top 10, and providing practical Java examples to help you protect your applications effectively.",{"type":22,"tag":30,"props":1773,"children":1775},{"id":1774},"understanding-owasp-top-10",[1776],{"type":27,"value":1777},"Understanding OWASP Top 10",{"type":22,"tag":23,"props":1779,"children":1780},{},[1781],{"type":27,"value":1782},"The OWASP Top 10 outlines the most critical web application security risks:",{"type":22,"tag":78,"props":1784,"children":1785},{},[1786,1791,1796,1801,1806,1811,1816,1821,1826,1831],{"type":22,"tag":46,"props":1787,"children":1788},{},[1789],{"type":27,"value":1790},"Injection",{"type":22,"tag":46,"props":1792,"children":1793},{},[1794],{"type":27,"value":1795},"Broken Authentication",{"type":22,"tag":46,"props":1797,"children":1798},{},[1799],{"type":27,"value":1800},"Sensitive Data Exposure",{"type":22,"tag":46,"props":1802,"children":1803},{},[1804],{"type":27,"value":1805},"XML External Entities (XXE)",{"type":22,"tag":46,"props":1807,"children":1808},{},[1809],{"type":27,"value":1810},"Broken Access Control",{"type":22,"tag":46,"props":1812,"children":1813},{},[1814],{"type":27,"value":1815},"Security Misconfiguration",{"type":22,"tag":46,"props":1817,"children":1818},{},[1819],{"type":27,"value":1820},"Cross-Site Scripting (XSS)",{"type":22,"tag":46,"props":1822,"children":1823},{},[1824],{"type":27,"value":1825},"Insecure Deserialization",{"type":22,"tag":46,"props":1827,"children":1828},{},[1829],{"type":27,"value":1830},"Using Components with Known Vulnerabilities",{"type":22,"tag":46,"props":1832,"children":1833},{},[1834],{"type":27,"value":1835},"Insufficient Logging and Monitoring",{"type":22,"tag":23,"props":1837,"children":1838},{},[1839],{"type":27,"value":1840},"Let's delve into practical strategies and Java implementations for some of these threats.",{"type":22,"tag":30,"props":1842,"children":1844},{"id":1843},"_1-injection-prevention",[1845],{"type":27,"value":1846},"1. Injection Prevention",{"type":22,"tag":23,"props":1848,"children":1849},{},[1850],{"type":27,"value":1851},"Use Prepared Statements and parameterized queries:",{"type":22,"tag":97,"props":1853,"children":1856},{"className":1854,"code":1855,"language":1758,"meta":7,"style":7},"language-java shiki shiki-themes github-dark github-light","PreparedStatement stmt = connection.prepareStatement(\"SELECT * FROM users WHERE username=? AND password=?\");\nstmt.setString(1, username);\nstmt.setString(2, password);\nResultSet rs = stmt.executeQuery();\n",[1857],{"type":22,"tag":14,"props":1858,"children":1859},{"__ignoreMap":7},[1860,1896,1923,1948],{"type":22,"tag":107,"props":1861,"children":1862},{"class":109,"line":110},[1863,1868,1872,1877,1882,1886,1891],{"type":22,"tag":107,"props":1864,"children":1865},{"style":480},[1866],{"type":27,"value":1867},"PreparedStatement stmt ",{"type":22,"tag":107,"props":1869,"children":1870},{"style":474},[1871],{"type":27,"value":619},{"type":22,"tag":107,"props":1873,"children":1874},{"style":480},[1875],{"type":27,"value":1876}," connection.",{"type":22,"tag":107,"props":1878,"children":1879},{"style":114},[1880],{"type":27,"value":1881},"prepareStatement",{"type":22,"tag":107,"props":1883,"children":1884},{"style":480},[1885],{"type":27,"value":1576},{"type":22,"tag":107,"props":1887,"children":1888},{"style":120},[1889],{"type":27,"value":1890},"\"SELECT * FROM users WHERE username=? AND password=?\"",{"type":22,"tag":107,"props":1892,"children":1893},{"style":480},[1894],{"type":27,"value":1895},");\n",{"type":22,"tag":107,"props":1897,"children":1898},{"class":109,"line":311},[1899,1904,1909,1913,1918],{"type":22,"tag":107,"props":1900,"children":1901},{"style":480},[1902],{"type":27,"value":1903},"stmt.",{"type":22,"tag":107,"props":1905,"children":1906},{"style":114},[1907],{"type":27,"value":1908},"setString",{"type":22,"tag":107,"props":1910,"children":1911},{"style":480},[1912],{"type":27,"value":1576},{"type":22,"tag":107,"props":1914,"children":1915},{"style":126},[1916],{"type":27,"value":1917},"1",{"type":22,"tag":107,"props":1919,"children":1920},{"style":480},[1921],{"type":27,"value":1922},", username);\n",{"type":22,"tag":107,"props":1924,"children":1925},{"class":109,"line":517},[1926,1930,1934,1938,1943],{"type":22,"tag":107,"props":1927,"children":1928},{"style":480},[1929],{"type":27,"value":1903},{"type":22,"tag":107,"props":1931,"children":1932},{"style":114},[1933],{"type":27,"value":1908},{"type":22,"tag":107,"props":1935,"children":1936},{"style":480},[1937],{"type":27,"value":1576},{"type":22,"tag":107,"props":1939,"children":1940},{"style":126},[1941],{"type":27,"value":1942},"2",{"type":22,"tag":107,"props":1944,"children":1945},{"style":480},[1946],{"type":27,"value":1947},", password);\n",{"type":22,"tag":107,"props":1949,"children":1950},{"class":109,"line":530},[1951,1956,1960,1965,1970],{"type":22,"tag":107,"props":1952,"children":1953},{"style":480},[1954],{"type":27,"value":1955},"ResultSet rs ",{"type":22,"tag":107,"props":1957,"children":1958},{"style":474},[1959],{"type":27,"value":619},{"type":22,"tag":107,"props":1961,"children":1962},{"style":480},[1963],{"type":27,"value":1964}," stmt.",{"type":22,"tag":107,"props":1966,"children":1967},{"style":114},[1968],{"type":27,"value":1969},"executeQuery",{"type":22,"tag":107,"props":1971,"children":1972},{"style":480},[1973],{"type":27,"value":1974},"();\n",{"type":22,"tag":23,"props":1976,"children":1977},{},[1978],{"type":27,"value":1979},"This approach prevents SQL injection by ensuring that input data is treated as data, not executable code.",{"type":22,"tag":30,"props":1981,"children":1983},{"id":1982},"_2-secure-authentication",[1984],{"type":27,"value":1985},"2. Secure Authentication",{"type":22,"tag":23,"props":1987,"children":1988},{},[1989],{"type":27,"value":1990},"Implement robust authentication using secure password storage (bcrypt or Argon2):",{"type":22,"tag":97,"props":1992,"children":1994},{"className":1854,"code":1993,"language":1758,"meta":7,"style":7},"import org.springframework.security.crypto.bcrypt.BCrypt;\n\nString hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());\nif (BCrypt.checkpw(password, hashedPassword)) {\n    // authentication successful\n}\n",[1995],{"type":22,"tag":14,"props":1996,"children":1997},{"__ignoreMap":7},[1998,2010,2017,2054,2077,2086],{"type":22,"tag":107,"props":1999,"children":2000},{"class":109,"line":110},[2001,2005],{"type":22,"tag":107,"props":2002,"children":2003},{"style":474},[2004],{"type":27,"value":488},{"type":22,"tag":107,"props":2006,"children":2007},{"style":480},[2008],{"type":27,"value":2009}," org.springframework.security.crypto.bcrypt.BCrypt;\n",{"type":22,"tag":107,"props":2011,"children":2012},{"class":109,"line":311},[2013],{"type":22,"tag":107,"props":2014,"children":2015},{"emptyLinePlaceholder":556},[2016],{"type":27,"value":559},{"type":22,"tag":107,"props":2018,"children":2019},{"class":109,"line":517},[2020,2025,2029,2034,2039,2044,2049],{"type":22,"tag":107,"props":2021,"children":2022},{"style":480},[2023],{"type":27,"value":2024},"String hashedPassword ",{"type":22,"tag":107,"props":2026,"children":2027},{"style":474},[2028],{"type":27,"value":619},{"type":22,"tag":107,"props":2030,"children":2031},{"style":480},[2032],{"type":27,"value":2033}," BCrypt.",{"type":22,"tag":107,"props":2035,"children":2036},{"style":114},[2037],{"type":27,"value":2038},"hashpw",{"type":22,"tag":107,"props":2040,"children":2041},{"style":480},[2042],{"type":27,"value":2043},"(password, BCrypt.",{"type":22,"tag":107,"props":2045,"children":2046},{"style":114},[2047],{"type":27,"value":2048},"gensalt",{"type":22,"tag":107,"props":2050,"children":2051},{"style":480},[2052],{"type":27,"value":2053},"());\n",{"type":22,"tag":107,"props":2055,"children":2056},{"class":109,"line":530},[2057,2062,2067,2072],{"type":22,"tag":107,"props":2058,"children":2059},{"style":474},[2060],{"type":27,"value":2061},"if",{"type":22,"tag":107,"props":2063,"children":2064},{"style":480},[2065],{"type":27,"value":2066}," (BCrypt.",{"type":22,"tag":107,"props":2068,"children":2069},{"style":114},[2070],{"type":27,"value":2071},"checkpw",{"type":22,"tag":107,"props":2073,"children":2074},{"style":480},[2075],{"type":27,"value":2076},"(password, hashedPassword)) {\n",{"type":22,"tag":107,"props":2078,"children":2079},{"class":109,"line":552},[2080],{"type":22,"tag":107,"props":2081,"children":2083},{"style":2082},"--shiki-default:#6A737D;--shiki-light:#6A737D",[2084],{"type":27,"value":2085},"    // authentication successful\n",{"type":22,"tag":107,"props":2087,"children":2088},{"class":109,"line":562},[2089],{"type":22,"tag":107,"props":2090,"children":2091},{"style":480},[2092],{"type":27,"value":2093},"}\n",{"type":22,"tag":30,"props":2095,"children":2097},{"id":2096},"_3-sensitive-data-exposure",[2098],{"type":27,"value":2099},"3. Sensitive Data Exposure",{"type":22,"tag":23,"props":2101,"children":2102},{},[2103],{"type":27,"value":2104},"Use encryption for sensitive data storage:",{"type":22,"tag":97,"props":2106,"children":2108},{"className":1854,"code":2107,"language":1758,"meta":7,"style":7},"Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nSecretKey key = generateSecretKey();\ncipher.init(Cipher.ENCRYPT_MODE, key);\nbyte[] encryptedData = cipher.doFinal(plainData.getBytes(StandardCharsets.UTF_8));\n",[2109],{"type":22,"tag":14,"props":2110,"children":2111},{"__ignoreMap":7},[2112,2147,2168,2186],{"type":22,"tag":107,"props":2113,"children":2114},{"class":109,"line":110},[2115,2120,2124,2129,2134,2138,2143],{"type":22,"tag":107,"props":2116,"children":2117},{"style":480},[2118],{"type":27,"value":2119},"Cipher cipher ",{"type":22,"tag":107,"props":2121,"children":2122},{"style":474},[2123],{"type":27,"value":619},{"type":22,"tag":107,"props":2125,"children":2126},{"style":480},[2127],{"type":27,"value":2128}," Cipher.",{"type":22,"tag":107,"props":2130,"children":2131},{"style":114},[2132],{"type":27,"value":2133},"getInstance",{"type":22,"tag":107,"props":2135,"children":2136},{"style":480},[2137],{"type":27,"value":1576},{"type":22,"tag":107,"props":2139,"children":2140},{"style":120},[2141],{"type":27,"value":2142},"\"AES/GCM/NoPadding\"",{"type":22,"tag":107,"props":2144,"children":2145},{"style":480},[2146],{"type":27,"value":1895},{"type":22,"tag":107,"props":2148,"children":2149},{"class":109,"line":311},[2150,2155,2159,2164],{"type":22,"tag":107,"props":2151,"children":2152},{"style":480},[2153],{"type":27,"value":2154},"SecretKey key ",{"type":22,"tag":107,"props":2156,"children":2157},{"style":474},[2158],{"type":27,"value":619},{"type":22,"tag":107,"props":2160,"children":2161},{"style":114},[2162],{"type":27,"value":2163}," generateSecretKey",{"type":22,"tag":107,"props":2165,"children":2166},{"style":480},[2167],{"type":27,"value":1974},{"type":22,"tag":107,"props":2169,"children":2170},{"class":109,"line":517},[2171,2176,2181],{"type":22,"tag":107,"props":2172,"children":2173},{"style":480},[2174],{"type":27,"value":2175},"cipher.",{"type":22,"tag":107,"props":2177,"children":2178},{"style":114},[2179],{"type":27,"value":2180},"init",{"type":22,"tag":107,"props":2182,"children":2183},{"style":480},[2184],{"type":27,"value":2185},"(Cipher.ENCRYPT_MODE, key);\n",{"type":22,"tag":107,"props":2187,"children":2188},{"class":109,"line":530},[2189,2194,2199,2203,2208,2213,2218,2223],{"type":22,"tag":107,"props":2190,"children":2191},{"style":474},[2192],{"type":27,"value":2193},"byte",{"type":22,"tag":107,"props":2195,"children":2196},{"style":480},[2197],{"type":27,"value":2198},"[] encryptedData ",{"type":22,"tag":107,"props":2200,"children":2201},{"style":474},[2202],{"type":27,"value":619},{"type":22,"tag":107,"props":2204,"children":2205},{"style":480},[2206],{"type":27,"value":2207}," cipher.",{"type":22,"tag":107,"props":2209,"children":2210},{"style":114},[2211],{"type":27,"value":2212},"doFinal",{"type":22,"tag":107,"props":2214,"children":2215},{"style":480},[2216],{"type":27,"value":2217},"(plainData.",{"type":22,"tag":107,"props":2219,"children":2220},{"style":114},[2221],{"type":27,"value":2222},"getBytes",{"type":22,"tag":107,"props":2224,"children":2225},{"style":480},[2226],{"type":27,"value":2227},"(StandardCharsets.UTF_8));\n",{"type":22,"tag":23,"props":2229,"children":2230},{},[2231],{"type":27,"value":2232},"Always encrypt data at rest and in transit (use HTTPS).",{"type":22,"tag":30,"props":2234,"children":2236},{"id":2235},"_4-security-misconfiguration",[2237],{"type":27,"value":2238},"4. Security Misconfiguration",{"type":22,"tag":23,"props":2240,"children":2241},{},[2242],{"type":27,"value":2243},"Adopt secure defaults, disable debugging, and avoid detailed error messages:",{"type":22,"tag":97,"props":2245,"children":2249},{"className":2246,"code":2247,"language":2248,"meta":7,"style":7},"language-properties shiki shiki-themes github-dark github-light","server.error.include-stacktrace=never\nserver.error.include-message=never\n","properties",[2250],{"type":22,"tag":14,"props":2251,"children":2252},{"__ignoreMap":7},[2253,2261],{"type":22,"tag":107,"props":2254,"children":2255},{"class":109,"line":110},[2256],{"type":22,"tag":107,"props":2257,"children":2258},{},[2259],{"type":27,"value":2260},"server.error.include-stacktrace=never\n",{"type":22,"tag":107,"props":2262,"children":2263},{"class":109,"line":311},[2264],{"type":22,"tag":107,"props":2265,"children":2266},{},[2267],{"type":27,"value":2268},"server.error.include-message=never\n",{"type":22,"tag":23,"props":2270,"children":2271},{},[2272],{"type":27,"value":2273},"Review and secure your Java application configuration regularly.",{"type":22,"tag":30,"props":2275,"children":2277},{"id":2276},"_5-cross-site-scripting-xss",[2278],{"type":27,"value":2279},"5. Cross-Site Scripting (XSS)",{"type":22,"tag":23,"props":2281,"children":2282},{},[2283],{"type":27,"value":2284},"Utilize proper encoding or frameworks that prevent XSS by default, such as Thymeleaf:",{"type":22,"tag":97,"props":2286,"children":2290},{"className":2287,"code":2288,"language":2289,"meta":7,"style":7},"language-html shiki shiki-themes github-dark github-light","\u003Cp th:text=\"${userInput}\">\u003C/p>\n","html",[2291],{"type":22,"tag":14,"props":2292,"children":2293},{"__ignoreMap":7},[2294],{"type":22,"tag":107,"props":2295,"children":2296},{"class":109,"line":110},[2297],{"type":22,"tag":107,"props":2298,"children":2299},{},[2300],{"type":27,"value":2288},{"type":22,"tag":23,"props":2302,"children":2303},{},[2304],{"type":27,"value":2305},"Avoid directly inserting user inputs into HTML without proper sanitization or encoding.",{"type":22,"tag":30,"props":2307,"children":2309},{"id":2308},"_6-insecure-deserialization",[2310],{"type":27,"value":2311},"6. Insecure Deserialization",{"type":22,"tag":23,"props":2313,"children":2314},{},[2315],{"type":27,"value":2316},"Limit deserialization to trusted sources only:",{"type":22,"tag":97,"props":2318,"children":2320},{"className":1854,"code":2319,"language":1758,"meta":7,"style":7},"ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"trusted_data.ser\"));\nMyObject obj = (MyObject) ois.readObject();\n",[2321],{"type":22,"tag":14,"props":2322,"children":2323},{"__ignoreMap":7},[2324,2374],{"type":22,"tag":107,"props":2325,"children":2326},{"class":109,"line":110},[2327,2332,2336,2341,2346,2350,2355,2360,2364,2369],{"type":22,"tag":107,"props":2328,"children":2329},{"style":480},[2330],{"type":27,"value":2331},"ObjectInputStream ois ",{"type":22,"tag":107,"props":2333,"children":2334},{"style":474},[2335],{"type":27,"value":619},{"type":22,"tag":107,"props":2337,"children":2338},{"style":474},[2339],{"type":27,"value":2340}," new",{"type":22,"tag":107,"props":2342,"children":2343},{"style":114},[2344],{"type":27,"value":2345}," ObjectInputStream",{"type":22,"tag":107,"props":2347,"children":2348},{"style":480},[2349],{"type":27,"value":1576},{"type":22,"tag":107,"props":2351,"children":2352},{"style":474},[2353],{"type":27,"value":2354},"new",{"type":22,"tag":107,"props":2356,"children":2357},{"style":114},[2358],{"type":27,"value":2359}," FileInputStream",{"type":22,"tag":107,"props":2361,"children":2362},{"style":480},[2363],{"type":27,"value":1576},{"type":22,"tag":107,"props":2365,"children":2366},{"style":120},[2367],{"type":27,"value":2368},"\"trusted_data.ser\"",{"type":22,"tag":107,"props":2370,"children":2371},{"style":480},[2372],{"type":27,"value":2373},"));\n",{"type":22,"tag":107,"props":2375,"children":2376},{"class":109,"line":311},[2377,2382,2386,2391,2396],{"type":22,"tag":107,"props":2378,"children":2379},{"style":480},[2380],{"type":27,"value":2381},"MyObject obj ",{"type":22,"tag":107,"props":2383,"children":2384},{"style":474},[2385],{"type":27,"value":619},{"type":22,"tag":107,"props":2387,"children":2388},{"style":480},[2389],{"type":27,"value":2390}," (MyObject) ois.",{"type":22,"tag":107,"props":2392,"children":2393},{"style":114},[2394],{"type":27,"value":2395},"readObject",{"type":22,"tag":107,"props":2397,"children":2398},{"style":480},[2399],{"type":27,"value":1974},{"type":22,"tag":23,"props":2401,"children":2402},{},[2403],{"type":27,"value":2404},"Always validate deserialized data.",{"type":22,"tag":30,"props":2406,"children":2408},{"id":2407},"_7-component-vulnerabilities",[2409],{"type":27,"value":2410},"7. Component Vulnerabilities",{"type":22,"tag":23,"props":2412,"children":2413},{},[2414],{"type":27,"value":2415},"Regularly update libraries and dependencies, and use tools like OWASP Dependency-Check to identify vulnerabilities:",{"type":22,"tag":97,"props":2417,"children":2421},{"className":2418,"code":2419,"language":2420,"meta":7,"style":7},"language-xml shiki shiki-themes github-dark github-light","\u003Cplugin>\n    \u003CgroupId>org.owasp\u003C/groupId>\n    \u003CartifactId>dependency-check-maven\u003C/artifactId>\n    \u003Cversion>9.0.9\u003C/version>\n\u003C/plugin>\n","xml",[2422],{"type":22,"tag":14,"props":2423,"children":2424},{"__ignoreMap":7},[2425,2444,2470,2495,2520],{"type":22,"tag":107,"props":2426,"children":2427},{"class":109,"line":110},[2428,2433,2439],{"type":22,"tag":107,"props":2429,"children":2430},{"style":480},[2431],{"type":27,"value":2432},"\u003C",{"type":22,"tag":107,"props":2434,"children":2436},{"style":2435},"--shiki-default:#85E89D;--shiki-light:#22863A",[2437],{"type":27,"value":2438},"plugin",{"type":22,"tag":107,"props":2440,"children":2441},{"style":480},[2442],{"type":27,"value":2443},">\n",{"type":22,"tag":107,"props":2445,"children":2446},{"class":109,"line":311},[2447,2452,2457,2462,2466],{"type":22,"tag":107,"props":2448,"children":2449},{"style":480},[2450],{"type":27,"value":2451},"    \u003C",{"type":22,"tag":107,"props":2453,"children":2454},{"style":2435},[2455],{"type":27,"value":2456},"groupId",{"type":22,"tag":107,"props":2458,"children":2459},{"style":480},[2460],{"type":27,"value":2461},">org.owasp\u003C/",{"type":22,"tag":107,"props":2463,"children":2464},{"style":2435},[2465],{"type":27,"value":2456},{"type":22,"tag":107,"props":2467,"children":2468},{"style":480},[2469],{"type":27,"value":2443},{"type":22,"tag":107,"props":2471,"children":2472},{"class":109,"line":517},[2473,2477,2482,2487,2491],{"type":22,"tag":107,"props":2474,"children":2475},{"style":480},[2476],{"type":27,"value":2451},{"type":22,"tag":107,"props":2478,"children":2479},{"style":2435},[2480],{"type":27,"value":2481},"artifactId",{"type":22,"tag":107,"props":2483,"children":2484},{"style":480},[2485],{"type":27,"value":2486},">dependency-check-maven\u003C/",{"type":22,"tag":107,"props":2488,"children":2489},{"style":2435},[2490],{"type":27,"value":2481},{"type":22,"tag":107,"props":2492,"children":2493},{"style":480},[2494],{"type":27,"value":2443},{"type":22,"tag":107,"props":2496,"children":2497},{"class":109,"line":530},[2498,2502,2507,2512,2516],{"type":22,"tag":107,"props":2499,"children":2500},{"style":480},[2501],{"type":27,"value":2451},{"type":22,"tag":107,"props":2503,"children":2504},{"style":2435},[2505],{"type":27,"value":2506},"version",{"type":22,"tag":107,"props":2508,"children":2509},{"style":480},[2510],{"type":27,"value":2511},">9.0.9\u003C/",{"type":22,"tag":107,"props":2513,"children":2514},{"style":2435},[2515],{"type":27,"value":2506},{"type":22,"tag":107,"props":2517,"children":2518},{"style":480},[2519],{"type":27,"value":2443},{"type":22,"tag":107,"props":2521,"children":2522},{"class":109,"line":552},[2523,2528,2532],{"type":22,"tag":107,"props":2524,"children":2525},{"style":480},[2526],{"type":27,"value":2527},"\u003C/",{"type":22,"tag":107,"props":2529,"children":2530},{"style":2435},[2531],{"type":27,"value":2438},{"type":22,"tag":107,"props":2533,"children":2534},{"style":480},[2535],{"type":27,"value":2443},{"type":22,"tag":23,"props":2537,"children":2538},{},[2539],{"type":27,"value":2540},"Execute dependency checks regularly in your CI/CD pipeline.",{"type":22,"tag":30,"props":2542,"children":2544},{"id":2543},"_8-logging-and-monitoring",[2545],{"type":27,"value":2546},"8. Logging and Monitoring",{"type":22,"tag":23,"props":2548,"children":2549},{},[2550],{"type":27,"value":2551},"Implement comprehensive logging and monitoring to detect and respond to security incidents:",{"type":22,"tag":97,"props":2553,"children":2555},{"className":1854,"code":2554,"language":1758,"meta":7,"style":7},"logger.warn(\"Failed login attempt for user: {}\", username);\n",[2556],{"type":22,"tag":14,"props":2557,"children":2558},{"__ignoreMap":7},[2559],{"type":22,"tag":107,"props":2560,"children":2561},{"class":109,"line":110},[2562,2567,2572,2576,2581],{"type":22,"tag":107,"props":2563,"children":2564},{"style":480},[2565],{"type":27,"value":2566},"logger.",{"type":22,"tag":107,"props":2568,"children":2569},{"style":114},[2570],{"type":27,"value":2571},"warn",{"type":22,"tag":107,"props":2573,"children":2574},{"style":480},[2575],{"type":27,"value":1576},{"type":22,"tag":107,"props":2577,"children":2578},{"style":120},[2579],{"type":27,"value":2580},"\"Failed login attempt for user: {}\"",{"type":22,"tag":107,"props":2582,"children":2583},{"style":480},[2584],{"type":27,"value":1922},{"type":22,"tag":23,"props":2586,"children":2587},{},[2588],{"type":27,"value":2589},"Aggregate logs centrally, and set up alerts for suspicious activities.",{"type":22,"tag":30,"props":2591,"children":2593},{"id":2592},"conclusion",[2594],{"type":27,"value":2595},"Conclusion",{"type":22,"tag":23,"props":2597,"children":2598},{},[2599],{"type":27,"value":2600},"Adopting these best practices can greatly enhance the security posture of your Java applications. Stay proactive by regularly reviewing security measures, updating dependencies, and monitoring your systems. Security is a continuous process, not a one-time setup.",{"type":22,"tag":1691,"props":2602,"children":2604},{"id":2603},"further-reading",[2605],{"type":27,"value":2606},"Further Reading",{"type":22,"tag":42,"props":2608,"children":2609},{},[2610,2620,2630],{"type":22,"tag":46,"props":2611,"children":2612},{},[2613],{"type":22,"tag":1704,"props":2614,"children":2617},{"href":2615,"rel":2616},"https://owasp.org/www-project-top-ten/",[1708],[2618],{"type":27,"value":2619},"OWASP Top 10",{"type":22,"tag":46,"props":2621,"children":2622},{},[2623],{"type":22,"tag":1704,"props":2624,"children":2627},{"href":2625,"rel":2626},"https://spring.io/projects/spring-security",[1708],[2628],{"type":27,"value":2629},"Spring Security",{"type":22,"tag":46,"props":2631,"children":2632},{},[2633],{"type":22,"tag":1704,"props":2634,"children":2637},{"href":2635,"rel":2636},"https://owasp.org/www-project-dependency-check/",[1708],[2638],{"type":27,"value":2639},"OWASP Dependency Check",{"type":22,"tag":305,"props":2641,"children":2642},{},[2643],{"type":27,"value":309},{"title":7,"searchDepth":311,"depth":311,"links":2645},[2646,2647,2648,2649,2650,2651,2652,2653,2654,2655],{"id":1774,"depth":311,"text":1777},{"id":1843,"depth":311,"text":1846},{"id":1982,"depth":311,"text":1985},{"id":2096,"depth":311,"text":2099},{"id":2235,"depth":311,"text":2238},{"id":2276,"depth":311,"text":2279},{"id":2308,"depth":311,"text":2311},{"id":2407,"depth":311,"text":2410},{"id":2543,"depth":311,"text":2546},{"id":2592,"depth":311,"text":2595},"content:posts:java-security-best-practices.md","posts/java-security-best-practices.md","posts/java-security-best-practices",{"_path":2660,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":2661,"description":2662,"date":2663,"layout":11,"subtitle":2664,"image":2665,"optimized_image":2665,"category":14,"tags":2666,"author":17,"paginate":6,"body":2670,"_type":316,"_id":3476,"_source":318,"_file":3477,"_stem":3478,"_extension":321},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","An expert guide to understanding and implementing circuit breakers in distributed systems, with practical Java examples using Resilience4j and best practices.","2024-07-15T11:00:00.000Z","Enhancing Resilience with Circuit Breaker Patterns","/assets/img/uploads/circuit-breaking-resilience.png",[14,1758,2667,2668,2669],"resilience","circuit-breaker","distributed-systems",{"type":19,"children":2671,"toc":3464},[2672,2680,2686,2691,2725,2730,2736,2741,2759,2765,2772,2777,2887,2893,3097,3103,3108,3275,3281,3286,3380,3385,3391,3414,3418,3423,3427,3460],{"type":22,"tag":339,"props":2673,"children":2674},{},[2675],{"type":22,"tag":23,"props":2676,"children":2677},{},[2678],{"type":27,"value":2679},"In modern distributed systems, failures are inevitable. The circuit breaker pattern helps maintain system stability by preventing failures from cascading across service boundaries. In this post, we'll dive deep into circuit breakers, explore their importance, and demonstrate practical implementation using Resilience4j in Java.",{"type":22,"tag":30,"props":2681,"children":2683},{"id":2682},"understanding-the-circuit-breaker-pattern",[2684],{"type":27,"value":2685},"Understanding the Circuit Breaker Pattern",{"type":22,"tag":23,"props":2687,"children":2688},{},[2689],{"type":27,"value":2690},"The circuit breaker pattern prevents a service from making repeated calls to an external service that is experiencing issues. It has three main states:",{"type":22,"tag":42,"props":2692,"children":2693},{},[2694,2705,2715],{"type":22,"tag":46,"props":2695,"children":2696},{},[2697,2703],{"type":22,"tag":2698,"props":2699,"children":2700},"strong",{},[2701],{"type":27,"value":2702},"Closed:",{"type":27,"value":2704}," All requests pass through normally.",{"type":22,"tag":46,"props":2706,"children":2707},{},[2708,2713],{"type":22,"tag":2698,"props":2709,"children":2710},{},[2711],{"type":27,"value":2712},"Open:",{"type":27,"value":2714}," Requests fail fast, bypassing the problematic service.",{"type":22,"tag":46,"props":2716,"children":2717},{},[2718,2723],{"type":22,"tag":2698,"props":2719,"children":2720},{},[2721],{"type":27,"value":2722},"Half-Open:",{"type":27,"value":2724}," A limited number of requests test if the external service has recovered.",{"type":22,"tag":23,"props":2726,"children":2727},{},[2728],{"type":27,"value":2729},"This mechanism helps improve the resilience and responsiveness of your systems.",{"type":22,"tag":30,"props":2731,"children":2733},{"id":2732},"when-to-use-circuit-breakers",[2734],{"type":27,"value":2735},"When to Use Circuit Breakers",{"type":22,"tag":23,"props":2737,"children":2738},{},[2739],{"type":27,"value":2740},"Circuit breakers are essential when:",{"type":22,"tag":42,"props":2742,"children":2743},{},[2744,2749,2754],{"type":22,"tag":46,"props":2745,"children":2746},{},[2747],{"type":27,"value":2748},"Your system relies on remote calls (microservices architecture, external APIs).",{"type":22,"tag":46,"props":2750,"children":2751},{},[2752],{"type":27,"value":2753},"You need to prevent failure propagation.",{"type":22,"tag":46,"props":2755,"children":2756},{},[2757],{"type":27,"value":2758},"You aim to improve user experience by quickly responding to failures.",{"type":22,"tag":30,"props":2760,"children":2762},{"id":2761},"implementing-circuit-breakers-with-resilience4j",[2763],{"type":27,"value":2764},"Implementing Circuit Breakers with Resilience4j",{"type":22,"tag":2766,"props":2767,"children":2769},"h3",{"id":2768},"step-1-adding-dependencies",[2770],{"type":27,"value":2771},"Step 1: Adding Dependencies",{"type":22,"tag":23,"props":2773,"children":2774},{},[2775],{"type":27,"value":2776},"Add Resilience4j dependencies to your project (Maven example):",{"type":22,"tag":97,"props":2778,"children":2780},{"className":2418,"code":2779,"language":2420,"meta":7,"style":7},"\u003Cdependency>\n    \u003CgroupId>io.github.resilience4j\u003C/groupId>\n    \u003CartifactId>resilience4j-circuitbreaker\u003C/artifactId>\n    \u003Cversion>2.0.2\u003C/version>\n\u003C/dependency>\n",[2781],{"type":22,"tag":14,"props":2782,"children":2783},{"__ignoreMap":7},[2784,2800,2824,2848,2872],{"type":22,"tag":107,"props":2785,"children":2786},{"class":109,"line":110},[2787,2791,2796],{"type":22,"tag":107,"props":2788,"children":2789},{"style":480},[2790],{"type":27,"value":2432},{"type":22,"tag":107,"props":2792,"children":2793},{"style":2435},[2794],{"type":27,"value":2795},"dependency",{"type":22,"tag":107,"props":2797,"children":2798},{"style":480},[2799],{"type":27,"value":2443},{"type":22,"tag":107,"props":2801,"children":2802},{"class":109,"line":311},[2803,2807,2811,2816,2820],{"type":22,"tag":107,"props":2804,"children":2805},{"style":480},[2806],{"type":27,"value":2451},{"type":22,"tag":107,"props":2808,"children":2809},{"style":2435},[2810],{"type":27,"value":2456},{"type":22,"tag":107,"props":2812,"children":2813},{"style":480},[2814],{"type":27,"value":2815},">io.github.resilience4j\u003C/",{"type":22,"tag":107,"props":2817,"children":2818},{"style":2435},[2819],{"type":27,"value":2456},{"type":22,"tag":107,"props":2821,"children":2822},{"style":480},[2823],{"type":27,"value":2443},{"type":22,"tag":107,"props":2825,"children":2826},{"class":109,"line":517},[2827,2831,2835,2840,2844],{"type":22,"tag":107,"props":2828,"children":2829},{"style":480},[2830],{"type":27,"value":2451},{"type":22,"tag":107,"props":2832,"children":2833},{"style":2435},[2834],{"type":27,"value":2481},{"type":22,"tag":107,"props":2836,"children":2837},{"style":480},[2838],{"type":27,"value":2839},">resilience4j-circuitbreaker\u003C/",{"type":22,"tag":107,"props":2841,"children":2842},{"style":2435},[2843],{"type":27,"value":2481},{"type":22,"tag":107,"props":2845,"children":2846},{"style":480},[2847],{"type":27,"value":2443},{"type":22,"tag":107,"props":2849,"children":2850},{"class":109,"line":530},[2851,2855,2859,2864,2868],{"type":22,"tag":107,"props":2852,"children":2853},{"style":480},[2854],{"type":27,"value":2451},{"type":22,"tag":107,"props":2856,"children":2857},{"style":2435},[2858],{"type":27,"value":2506},{"type":22,"tag":107,"props":2860,"children":2861},{"style":480},[2862],{"type":27,"value":2863},">2.0.2\u003C/",{"type":22,"tag":107,"props":2865,"children":2866},{"style":2435},[2867],{"type":27,"value":2506},{"type":22,"tag":107,"props":2869,"children":2870},{"style":480},[2871],{"type":27,"value":2443},{"type":22,"tag":107,"props":2873,"children":2874},{"class":109,"line":552},[2875,2879,2883],{"type":22,"tag":107,"props":2876,"children":2877},{"style":480},[2878],{"type":27,"value":2527},{"type":22,"tag":107,"props":2880,"children":2881},{"style":2435},[2882],{"type":27,"value":2795},{"type":22,"tag":107,"props":2884,"children":2885},{"style":480},[2886],{"type":27,"value":2443},{"type":22,"tag":2766,"props":2888,"children":2890},{"id":2889},"step-2-configuring-circuit-breaker",[2891],{"type":27,"value":2892},"Step 2: Configuring Circuit Breaker",{"type":22,"tag":97,"props":2894,"children":2896},{"className":1854,"code":2895,"language":1758,"meta":7,"style":7},"CircuitBreakerConfig config = CircuitBreakerConfig.custom()\n    .failureRateThreshold(50)\n    .waitDurationInOpenState(Duration.ofMillis(1000))\n    .permittedNumberOfCallsInHalfOpenState(2)\n    .minimumNumberOfCalls(5)\n    .build();\n\nCircuitBreaker circuitBreaker = CircuitBreaker.of(\"myService\", config);\n",[2897],{"type":22,"tag":14,"props":2898,"children":2899},{"__ignoreMap":7},[2900,2927,2953,2989,3013,3038,3054,3061],{"type":22,"tag":107,"props":2901,"children":2902},{"class":109,"line":110},[2903,2908,2912,2917,2922],{"type":22,"tag":107,"props":2904,"children":2905},{"style":480},[2906],{"type":27,"value":2907},"CircuitBreakerConfig config ",{"type":22,"tag":107,"props":2909,"children":2910},{"style":474},[2911],{"type":27,"value":619},{"type":22,"tag":107,"props":2913,"children":2914},{"style":480},[2915],{"type":27,"value":2916}," CircuitBreakerConfig.",{"type":22,"tag":107,"props":2918,"children":2919},{"style":114},[2920],{"type":27,"value":2921},"custom",{"type":22,"tag":107,"props":2923,"children":2924},{"style":480},[2925],{"type":27,"value":2926},"()\n",{"type":22,"tag":107,"props":2928,"children":2929},{"class":109,"line":311},[2930,2935,2940,2944,2949],{"type":22,"tag":107,"props":2931,"children":2932},{"style":480},[2933],{"type":27,"value":2934},"    .",{"type":22,"tag":107,"props":2936,"children":2937},{"style":114},[2938],{"type":27,"value":2939},"failureRateThreshold",{"type":22,"tag":107,"props":2941,"children":2942},{"style":480},[2943],{"type":27,"value":1576},{"type":22,"tag":107,"props":2945,"children":2946},{"style":126},[2947],{"type":27,"value":2948},"50",{"type":22,"tag":107,"props":2950,"children":2951},{"style":480},[2952],{"type":27,"value":605},{"type":22,"tag":107,"props":2954,"children":2955},{"class":109,"line":517},[2956,2960,2965,2970,2975,2979,2984],{"type":22,"tag":107,"props":2957,"children":2958},{"style":480},[2959],{"type":27,"value":2934},{"type":22,"tag":107,"props":2961,"children":2962},{"style":114},[2963],{"type":27,"value":2964},"waitDurationInOpenState",{"type":22,"tag":107,"props":2966,"children":2967},{"style":480},[2968],{"type":27,"value":2969},"(Duration.",{"type":22,"tag":107,"props":2971,"children":2972},{"style":114},[2973],{"type":27,"value":2974},"ofMillis",{"type":22,"tag":107,"props":2976,"children":2977},{"style":480},[2978],{"type":27,"value":1576},{"type":22,"tag":107,"props":2980,"children":2981},{"style":126},[2982],{"type":27,"value":2983},"1000",{"type":22,"tag":107,"props":2985,"children":2986},{"style":480},[2987],{"type":27,"value":2988},"))\n",{"type":22,"tag":107,"props":2990,"children":2991},{"class":109,"line":530},[2992,2996,3001,3005,3009],{"type":22,"tag":107,"props":2993,"children":2994},{"style":480},[2995],{"type":27,"value":2934},{"type":22,"tag":107,"props":2997,"children":2998},{"style":114},[2999],{"type":27,"value":3000},"permittedNumberOfCallsInHalfOpenState",{"type":22,"tag":107,"props":3002,"children":3003},{"style":480},[3004],{"type":27,"value":1576},{"type":22,"tag":107,"props":3006,"children":3007},{"style":126},[3008],{"type":27,"value":1942},{"type":22,"tag":107,"props":3010,"children":3011},{"style":480},[3012],{"type":27,"value":605},{"type":22,"tag":107,"props":3014,"children":3015},{"class":109,"line":552},[3016,3020,3025,3029,3034],{"type":22,"tag":107,"props":3017,"children":3018},{"style":480},[3019],{"type":27,"value":2934},{"type":22,"tag":107,"props":3021,"children":3022},{"style":114},[3023],{"type":27,"value":3024},"minimumNumberOfCalls",{"type":22,"tag":107,"props":3026,"children":3027},{"style":480},[3028],{"type":27,"value":1576},{"type":22,"tag":107,"props":3030,"children":3031},{"style":126},[3032],{"type":27,"value":3033},"5",{"type":22,"tag":107,"props":3035,"children":3036},{"style":480},[3037],{"type":27,"value":605},{"type":22,"tag":107,"props":3039,"children":3040},{"class":109,"line":562},[3041,3045,3050],{"type":22,"tag":107,"props":3042,"children":3043},{"style":480},[3044],{"type":27,"value":2934},{"type":22,"tag":107,"props":3046,"children":3047},{"style":114},[3048],{"type":27,"value":3049},"build",{"type":22,"tag":107,"props":3051,"children":3052},{"style":480},[3053],{"type":27,"value":1974},{"type":22,"tag":107,"props":3055,"children":3056},{"class":109,"line":571},[3057],{"type":22,"tag":107,"props":3058,"children":3059},{"emptyLinePlaceholder":556},[3060],{"type":27,"value":559},{"type":22,"tag":107,"props":3062,"children":3063},{"class":109,"line":579},[3064,3069,3073,3078,3083,3087,3092],{"type":22,"tag":107,"props":3065,"children":3066},{"style":480},[3067],{"type":27,"value":3068},"CircuitBreaker circuitBreaker ",{"type":22,"tag":107,"props":3070,"children":3071},{"style":474},[3072],{"type":27,"value":619},{"type":22,"tag":107,"props":3074,"children":3075},{"style":480},[3076],{"type":27,"value":3077}," CircuitBreaker.",{"type":22,"tag":107,"props":3079,"children":3080},{"style":114},[3081],{"type":27,"value":3082},"of",{"type":22,"tag":107,"props":3084,"children":3085},{"style":480},[3086],{"type":27,"value":1576},{"type":22,"tag":107,"props":3088,"children":3089},{"style":120},[3090],{"type":27,"value":3091},"\"myService\"",{"type":22,"tag":107,"props":3093,"children":3094},{"style":480},[3095],{"type":27,"value":3096},", config);\n",{"type":22,"tag":2766,"props":3098,"children":3100},{"id":3099},"step-3-using-circuit-breaker",[3101],{"type":27,"value":3102},"Step 3: Using Circuit Breaker",{"type":22,"tag":23,"props":3104,"children":3105},{},[3106],{"type":27,"value":3107},"Wrap service calls using the circuit breaker:",{"type":22,"tag":97,"props":3109,"children":3111},{"className":1854,"code":3110,"language":1758,"meta":7,"style":7},"Supplier\u003CString> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> myService.remoteCall());\n\nTry.ofSupplier(decoratedSupplier)\n    .recover(throwable -> \"Fallback response\")\n    .onSuccess(result -> System.out.println(\"Response: \" + result));\n",[3112],{"type":22,"tag":14,"props":3113,"children":3114},{"__ignoreMap":7},[3115,3170,3177,3195,3225],{"type":22,"tag":107,"props":3116,"children":3117},{"class":109,"line":110},[3118,3123,3128,3133,3137,3141,3146,3151,3156,3161,3166],{"type":22,"tag":107,"props":3119,"children":3120},{"style":480},[3121],{"type":27,"value":3122},"Supplier\u003C",{"type":22,"tag":107,"props":3124,"children":3125},{"style":474},[3126],{"type":27,"value":3127},"String",{"type":22,"tag":107,"props":3129,"children":3130},{"style":480},[3131],{"type":27,"value":3132},"> decoratedSupplier ",{"type":22,"tag":107,"props":3134,"children":3135},{"style":474},[3136],{"type":27,"value":619},{"type":22,"tag":107,"props":3138,"children":3139},{"style":480},[3140],{"type":27,"value":3077},{"type":22,"tag":107,"props":3142,"children":3143},{"style":114},[3144],{"type":27,"value":3145},"decorateSupplier",{"type":22,"tag":107,"props":3147,"children":3148},{"style":480},[3149],{"type":27,"value":3150},"(circuitBreaker, () ",{"type":22,"tag":107,"props":3152,"children":3153},{"style":474},[3154],{"type":27,"value":3155},"->",{"type":22,"tag":107,"props":3157,"children":3158},{"style":480},[3159],{"type":27,"value":3160}," myService.",{"type":22,"tag":107,"props":3162,"children":3163},{"style":114},[3164],{"type":27,"value":3165},"remoteCall",{"type":22,"tag":107,"props":3167,"children":3168},{"style":480},[3169],{"type":27,"value":2053},{"type":22,"tag":107,"props":3171,"children":3172},{"class":109,"line":311},[3173],{"type":22,"tag":107,"props":3174,"children":3175},{"emptyLinePlaceholder":556},[3176],{"type":27,"value":559},{"type":22,"tag":107,"props":3178,"children":3179},{"class":109,"line":517},[3180,3185,3190],{"type":22,"tag":107,"props":3181,"children":3182},{"style":480},[3183],{"type":27,"value":3184},"Try.",{"type":22,"tag":107,"props":3186,"children":3187},{"style":114},[3188],{"type":27,"value":3189},"ofSupplier",{"type":22,"tag":107,"props":3191,"children":3192},{"style":480},[3193],{"type":27,"value":3194},"(decoratedSupplier)\n",{"type":22,"tag":107,"props":3196,"children":3197},{"class":109,"line":530},[3198,3202,3207,3212,3216,3221],{"type":22,"tag":107,"props":3199,"children":3200},{"style":480},[3201],{"type":27,"value":2934},{"type":22,"tag":107,"props":3203,"children":3204},{"style":114},[3205],{"type":27,"value":3206},"recover",{"type":22,"tag":107,"props":3208,"children":3209},{"style":480},[3210],{"type":27,"value":3211},"(throwable ",{"type":22,"tag":107,"props":3213,"children":3214},{"style":474},[3215],{"type":27,"value":3155},{"type":22,"tag":107,"props":3217,"children":3218},{"style":120},[3219],{"type":27,"value":3220}," \"Fallback response\"",{"type":22,"tag":107,"props":3222,"children":3223},{"style":480},[3224],{"type":27,"value":605},{"type":22,"tag":107,"props":3226,"children":3227},{"class":109,"line":552},[3228,3232,3237,3242,3246,3251,3256,3260,3265,3270],{"type":22,"tag":107,"props":3229,"children":3230},{"style":480},[3231],{"type":27,"value":2934},{"type":22,"tag":107,"props":3233,"children":3234},{"style":114},[3235],{"type":27,"value":3236},"onSuccess",{"type":22,"tag":107,"props":3238,"children":3239},{"style":480},[3240],{"type":27,"value":3241},"(result ",{"type":22,"tag":107,"props":3243,"children":3244},{"style":474},[3245],{"type":27,"value":3155},{"type":22,"tag":107,"props":3247,"children":3248},{"style":480},[3249],{"type":27,"value":3250}," System.out.",{"type":22,"tag":107,"props":3252,"children":3253},{"style":114},[3254],{"type":27,"value":3255},"println",{"type":22,"tag":107,"props":3257,"children":3258},{"style":480},[3259],{"type":27,"value":1576},{"type":22,"tag":107,"props":3261,"children":3262},{"style":120},[3263],{"type":27,"value":3264},"\"Response: \"",{"type":22,"tag":107,"props":3266,"children":3267},{"style":474},[3268],{"type":27,"value":3269}," +",{"type":22,"tag":107,"props":3271,"children":3272},{"style":480},[3273],{"type":27,"value":3274}," result));\n",{"type":22,"tag":30,"props":3276,"children":3278},{"id":3277},"monitoring-and-metrics",[3279],{"type":27,"value":3280},"Monitoring and Metrics",{"type":22,"tag":23,"props":3282,"children":3283},{},[3284],{"type":27,"value":3285},"Resilience4j provides built-in support for metrics and monitoring:",{"type":22,"tag":97,"props":3287,"children":3289},{"className":1854,"code":3288,"language":1758,"meta":7,"style":7},"CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();\nregistry.getEventPublisher()\n    .onStateTransition(event -> System.out.println(event.getStateTransition()));\n",[3290],{"type":22,"tag":14,"props":3291,"children":3292},{"__ignoreMap":7},[3293,3319,3336],{"type":22,"tag":107,"props":3294,"children":3295},{"class":109,"line":110},[3296,3301,3305,3310,3315],{"type":22,"tag":107,"props":3297,"children":3298},{"style":480},[3299],{"type":27,"value":3300},"CircuitBreakerRegistry registry ",{"type":22,"tag":107,"props":3302,"children":3303},{"style":474},[3304],{"type":27,"value":619},{"type":22,"tag":107,"props":3306,"children":3307},{"style":480},[3308],{"type":27,"value":3309}," CircuitBreakerRegistry.",{"type":22,"tag":107,"props":3311,"children":3312},{"style":114},[3313],{"type":27,"value":3314},"ofDefaults",{"type":22,"tag":107,"props":3316,"children":3317},{"style":480},[3318],{"type":27,"value":1974},{"type":22,"tag":107,"props":3320,"children":3321},{"class":109,"line":311},[3322,3327,3332],{"type":22,"tag":107,"props":3323,"children":3324},{"style":480},[3325],{"type":27,"value":3326},"registry.",{"type":22,"tag":107,"props":3328,"children":3329},{"style":114},[3330],{"type":27,"value":3331},"getEventPublisher",{"type":22,"tag":107,"props":3333,"children":3334},{"style":480},[3335],{"type":27,"value":2926},{"type":22,"tag":107,"props":3337,"children":3338},{"class":109,"line":517},[3339,3343,3348,3353,3357,3361,3365,3370,3375],{"type":22,"tag":107,"props":3340,"children":3341},{"style":480},[3342],{"type":27,"value":2934},{"type":22,"tag":107,"props":3344,"children":3345},{"style":114},[3346],{"type":27,"value":3347},"onStateTransition",{"type":22,"tag":107,"props":3349,"children":3350},{"style":480},[3351],{"type":27,"value":3352},"(event ",{"type":22,"tag":107,"props":3354,"children":3355},{"style":474},[3356],{"type":27,"value":3155},{"type":22,"tag":107,"props":3358,"children":3359},{"style":480},[3360],{"type":27,"value":3250},{"type":22,"tag":107,"props":3362,"children":3363},{"style":114},[3364],{"type":27,"value":3255},{"type":22,"tag":107,"props":3366,"children":3367},{"style":480},[3368],{"type":27,"value":3369},"(event.",{"type":22,"tag":107,"props":3371,"children":3372},{"style":114},[3373],{"type":27,"value":3374},"getStateTransition",{"type":22,"tag":107,"props":3376,"children":3377},{"style":480},[3378],{"type":27,"value":3379},"()));\n",{"type":22,"tag":23,"props":3381,"children":3382},{},[3383],{"type":27,"value":3384},"Integrate with Prometheus, Grafana, or other observability tools for comprehensive monitoring.",{"type":22,"tag":30,"props":3386,"children":3388},{"id":3387},"best-practices-for-circuit-breaking",[3389],{"type":27,"value":3390},"Best Practices for Circuit Breaking",{"type":22,"tag":42,"props":3392,"children":3393},{},[3394,3399,3404,3409],{"type":22,"tag":46,"props":3395,"children":3396},{},[3397],{"type":27,"value":3398},"Configure sensible thresholds to avoid premature circuit opening.",{"type":22,"tag":46,"props":3400,"children":3401},{},[3402],{"type":27,"value":3403},"Always define fallback mechanisms.",{"type":22,"tag":46,"props":3405,"children":3406},{},[3407],{"type":27,"value":3408},"Regularly monitor circuit breaker states and metrics.",{"type":22,"tag":46,"props":3410,"children":3411},{},[3412],{"type":27,"value":3413},"Test thoroughly to ensure reliability under failure conditions.",{"type":22,"tag":30,"props":3415,"children":3416},{"id":2592},[3417],{"type":27,"value":2595},{"type":22,"tag":23,"props":3419,"children":3420},{},[3421],{"type":27,"value":3422},"Implementing circuit breakers using Resilience4j significantly enhances the resilience of your Java-based distributed systems. This proactive approach ensures your systems gracefully handle failures, maintaining service availability and user satisfaction.",{"type":22,"tag":1691,"props":3424,"children":3425},{"id":2603},[3426],{"type":27,"value":2606},{"type":22,"tag":42,"props":3428,"children":3429},{},[3430,3440,3450],{"type":22,"tag":46,"props":3431,"children":3432},{},[3433],{"type":22,"tag":1704,"props":3434,"children":3437},{"href":3435,"rel":3436},"https://resilience4j.readme.io/docs",[1708],[3438],{"type":27,"value":3439},"Resilience4j Official Documentation",{"type":22,"tag":46,"props":3441,"children":3442},{},[3443],{"type":22,"tag":1704,"props":3444,"children":3447},{"href":3445,"rel":3446},"https://martinfowler.com/bliki/CircuitBreaker.html",[1708],[3448],{"type":27,"value":3449},"Circuit Breaker Pattern by Martin Fowler",{"type":22,"tag":46,"props":3451,"children":3452},{},[3453],{"type":22,"tag":1704,"props":3454,"children":3457},{"href":3455,"rel":3456},"https://microservices.io/patterns/reliability/circuit-breaker.html",[1708],[3458],{"type":27,"value":3459},"Microservices Patterns by Chris Richardson",{"type":22,"tag":305,"props":3461,"children":3462},{},[3463],{"type":27,"value":309},{"title":7,"searchDepth":311,"depth":311,"links":3465},[3466,3467,3468,3473,3474,3475],{"id":2682,"depth":311,"text":2685},{"id":2732,"depth":311,"text":2735},{"id":2761,"depth":311,"text":2764,"children":3469},[3470,3471,3472],{"id":2768,"depth":517,"text":2771},{"id":2889,"depth":517,"text":2892},{"id":3099,"depth":517,"text":3102},{"id":3277,"depth":311,"text":3280},{"id":3387,"depth":311,"text":3390},{"id":2592,"depth":311,"text":2595},"content:posts:circuit-breaking-resilience.md","posts/circuit-breaking-resilience.md","posts/circuit-breaking-resilience",[3480,3481,3482,3483,3487,3491,3495,3499,3503,3507,3511,3515,3519,3523,3524,3528,3532,3536,3539,3543,3547],{"_path":324,"title":325,"date":327},{"_path":1751,"title":1752,"date":1754},{"_path":2660,"title":2661,"date":2663},{"_path":3484,"title":3485,"date":3486},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":3488,"title":3489,"date":3490},"/posts/pattern-matching-sealed-classes-java21","A Guide to Pattern Matching and Sealed Classes in Java 21","2024-05-15T11:00:00.000Z",{"_path":3492,"title":3493,"date":3494},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":3496,"title":3497,"date":3498},"/posts/mastering-concurrency","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","2024-03-15T11:00:00.000Z",{"_path":3500,"title":3501,"date":3502},"/posts/microservices-mistakes","Common Mistakes When Developing Microservices","2024-02-15T11:00:00.000Z",{"_path":3504,"title":3505,"date":3506},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":3508,"title":3509,"date":3510},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":3512,"title":3513,"date":3514},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":3516,"title":3517,"date":3518},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":3520,"title":3521,"date":3522},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":4,"title":8,"date":10},{"_path":3525,"title":3526,"date":3527},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":3529,"title":3530,"date":3531},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":3533,"title":3534,"date":3535},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","2020-12-06T11:00:00.000Z",{"_path":3537,"title":3538,"date":3535},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot",{"_path":3540,"title":3541,"date":3542},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":3544,"title":3545,"date":3546},"/posts/algorithms-working-with-trees","Algorithms - Binary tree traversals in kotlin","2020-11-24T11:00:00.000Z",{"_path":3548,"title":3549,"date":3550},"/posts/playing-with-ocr-libraries","Playing with OCR libraries","2020-11-22T11:00:00.000Z",1790510065895]