import java.sql.*;
import javax.sql.*;

public class MariaDbPoolBugRepro {
    public static void main(String[] args) throws Exception {
        // Replace with your DB setup
        String url = "jdbc:mariadb://localhost:3306/mydb";
        String user = "myuser";
        String password = "mypassword";

        // Setup MariaDB Pooled DataSource
        org.mariadb.jdbc.MariaDbDataSource ds = new org.mariadb.jdbc.MariaDbDataSource();
        ds.setUrl(url);
        ds.setUser(user);
        ds.setPassword(password);

        // Get pooled connection
        PooledConnection pooledConn = ds.getPooledConnection();
        pooledConn.addConnectionEventListener(new ConnectionEventListener() {
            @Override
            public void connectionClosed(ConnectionEvent event) {
                System.out.println("POOL: Logical connection returned to pool.");
            }
            @Override
            public void connectionErrorOccurred(ConnectionEvent event) {
                System.out.println("POOL: Connection error");
            }
        });

        // Borrow and use connection
        Connection con1 = pooledConn.getConnection();
        Statement st = con1.createStatement();
        st.execute("SELECT 1");
        st.close();
        // Return connection to pool
        con1.close();
        // POOL expects physical connection to stay open for next borrow...

        // Borrow a second time
        try {
            Connection con2 = pooledConn.getConnection();
            Statement st2 = con2.createStatement();
            st2.execute("SELECT 1"); // <-- Fails: connection is already closed
            st2.close();
            con2.close();
        } catch (SQLException e) {
            System.out.println("Second borrow failed: " + e);
        }

        pooledConn.close(); // Clean up
    }
}