Skip to content

Instantly share code, notes, and snippets.

@prayagupa
Last active August 4, 2020 19:16
Show Gist options
  • Select an option

  • Save prayagupa/50fcf537c89b1e151bf4 to your computer and use it in GitHub Desktop.

Select an option

Save prayagupa/50fcf537c89b1e151bf4 to your computer and use it in GitHub Desktop.
mysql, db, oracle
public class CustomerOrderEventHandler extends MultiEventHandler {

    private static Logger logger = LogManager.getLogger(CustomerOrderEventHandler.class);

    //private BasicDataSource establishedConnections = new BasicDataSource();

    //private DB2SimpleDataSource nativeEstablishedConnections = new DB2SimpleDataSource();

    private AS400JDBCManagedConnectionPoolDataSource dynamicEstablishedConnections =
            new AS400JDBCManagedConnectionPoolDataSource();

    private State3 orderState3;
    private State2 orderState2;
    private State1 orderState1;

    public CustomerOrderEventHandler() throws SQLException {
        connectionPool3();
        Connection connection = dynamicEstablishedConnections.getConnection();
        connection.close();
    }

    private void connectionPool3() {
        dynamicEstablishedConnections.setServerName(State.server);
        dynamicEstablishedConnections.setDatabaseName(State.DATABASE);
        dynamicEstablishedConnections.setUser(State.user);
        dynamicEstablishedConnections.setPassword(State.password);
        dynamicEstablishedConnections.setSavePasswordWhenSerialized(true);
        dynamicEstablishedConnections.setPrompt(false);
        dynamicEstablishedConnections.setMinPoolSize(3);
        dynamicEstablishedConnections.setInitialPoolSize(5);
        dynamicEstablishedConnections.setMaxPoolSize(50);
    }

    public void onEvent(CustomerOrder orderEvent){
        long start =  System.currentTimeMillis();
        Connection dbConnection = null;
        try {
            dbConnection = dynamicEstablishedConnections.getConnection();
            long connectionSetupTime = System.currentTimeMillis() - start;

            state3 = new State3(dbConnection);
            state2 = new State2(dbConnection);
            state1 = new State1(dbConnection);

            long initialisation = System.currentTimeMillis() - start - connectionSetupTime;

            int[] state3Result = state3.apply(orderEvent);
            int[] state2Result = state2.apply(orderEvent);
            long state1Result = state1.apply(orderEvent);

            dbConnection.commit();

            logger.info("eventId="+ getEventId(orderEvent) +
                    ",connectionSetupTime=" + connectionSetupTime +
                    ",queryPreCompilation=" + initialisation +
                    ",insertionOnlyTimeTaken=" +
                    (System.currentTimeMillis() - (start + connectionSetupTime + initialisation)) +
                    ",insertionTotalTimeTaken=" + (System.currentTimeMillis() - start));
        } catch (SQLException e) {
            logger.error("Error updating the order states.", e);
            if(dbConnection != null) {
                try {
                    dbConnection.rollback();
                } catch (SQLException e1) {
                    logger.error("Error rolling back the state.", e1);
                }
            }
            throw new CustomerOrderEventHandlerRuntimeException("Error updating the customer order states.", e);
        }
    }

    private Long getEventId(CustomerOrder order) {
        return Long.valueOf(order.getMessageHeader().getCorrelationId());
    }
}
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class State2 extends State {

    private static Logger logger = LogManager.getLogger(DetailState.class);

    Connection connection;
    PreparedStatement preparedStatement;

    String detailsCompiledQuery = "INSERT INTO " + DATABASE + "." + getStateName() +
            "(" + DetailState.EVENT_ID + ", " +
            State2.ORDER_NUMBER + ", " +
            State2.SKU_ID + ", " +
            State2.SKU_ORDERED_QTY + ") VALUES(?, ?, ?, ?)";

    public State2(Connection connection) throws SQLException {
        this.connection = connection;
        this.preparedStatement = this.connection.prepareStatement(detailsCompiledQuery); // this is taking ~200ms each time
        this.preparedStatement.setPoolable(true); //might not be required, not sure
    }

    public int[] apply(CustomerOrder event) throws StateException {

        event.getMessageBody().getDetails().forEach(detail -> {
            try {
                preparedStatement.setLong(1, getEventId(event));
                preparedStatement.setString(2, getOrderNo(event));
                preparedStatement.setInt(3, detail.getSkuId());
                preparedStatement.setInt(4, detail.getQty());
                preparedStatement.addBatch();
            } catch (SQLException e) {
                logger.error(e);
                throw new StateException("Error setting up data", e);
            }
        });

        long startedTime = System.currentTimeMillis();
        int[] inserted = new int[0];
        try {
            inserted = preparedStatement.executeBatch();
        } catch (SQLException e) {
            throw new StateException("Error updating allocations data", e);
        }
        logger.info("eventId="+ getEventId(event) +
                ",state=details,insertionTimeTaken=" + (System.currentTimeMillis() - startedTime));
        return inserted;
    }

    @Override
    protected String getStateName() {
        return properties.getProperty("state.order.details.name");
    }
}

auto reconnect to database fails once connection is lost

{
  "timeMillis": 1490720504955,
  "thread": "RecordProcessor-0003",
  "level": "ERROR",
  "loggerName": "com.eventstream.consumer.dispatcher.MultiEventHandler",
  "message": "error dispatching event, {}",
  "thrown": {
    "commonElementCount": 0,
    "name": "java.lang.reflect.InvocationTargetException",
    "cause": {
      "commonElementCount": 18,
      "localizedMessage": "serverName: Property was not changed.",
      "message": "serverName: Property was not changed.",
      "name": "com.ibm.as400.access.ExtendedIllegalStateException",
      "extendedStackTrace": [
        {
          "class": "com.ibm.as400.access.AS400JDBCManagedDataSource",
          "method": "setServerName",
          "file": "AS400JDBCManagedDataSource.java",
          "line": 3679,
          "exact": false,
          "location": "DB2DriverAS400-1.0.0.jar",
          "version": "JTOpen 8.2"
        },
        {
          "class": "com.nordstrom.purchaseorder.events.handler.PurchaseOrderEventHandler",
          "method": "setupConnectionPool",
          "file": "PurchaseOrderEventHandler.java",
          "line": 43,
          "exact": false,
          "location": "classes/",
          "version": "?"
        },
        {
          "class": "com.nordstrom.purchaseorder.events.handler.PurchaseOrderEventHandler",
          "method": "onEvent",
          "file": "PurchaseOrderEventHandler.java",
          "line": 82,
          "exact": false,
          "location": "classes/",
          "version": "?"
        }
      ]
    },
    "extendedStackTrace": [
      {
        "class": "sun.reflect.NativeMethodAccessorImpl",
        "method": "invoke0",
        "file": "NativeMethodAccessorImpl.java",
        "line": -2,
        "exact": false,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "sun.reflect.NativeMethodAccessorImpl",
        "method": "invoke",
        "file": "NativeMethodAccessorImpl.java",
        "line": 62,
        "exact": false,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "sun.reflect.DelegatingMethodAccessorImpl",
        "method": "invoke",
        "file": "DelegatingMethodAccessorImpl.java",
        "line": 43,
        "exact": false,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "java.lang.reflect.Method",
        "method": "invoke",
        "file": "Method.java",
        "line": 498,
        "exact": false,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "com.eventstream.consumer.dispatcher.MultiEventHandler",
        "method": "lambda$dispatchEvent$1",
        "file": "MultiEventHandler.java",
        "line": 43,
        "exact": false,
        "location": "stream-driver-1.0-SNAPSHOT.jar",
        "version": "?"
      },
      {
        "class": "java.util.Optional",
        "method": "ifPresent",
        "file": "Optional.java",
        "line": 159,
        "exact": false,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "com.eventstream.consumer.dispatcher.MultiEventHandler",
        "method": "dispatchEvent",
        "file": "MultiEventHandler.java",
        "line": 41,
        "exact": false,
        "location": "stream-driver-1.0-SNAPSHOT.jar",
        "version": "?"
      },
      {
        "class": "com.eventstream.consumer.kinesis.KinesisEventPartitionProcessor",
        "method": "lambda$processRecords$0",
        "file": "KinesisEventPartitionProcessor.java",
        "line": 160,
        "exact": false,
        "location": "stream-driver-1.0-SNAPSHOT.jar",
        "version": "?"
      },
      {
        "class": "java.util.ArrayList",
        "method": "forEach",
        "file": "ArrayList.java",
        "line": 1249,
        "exact": true,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "com.eventstream.consumer.kinesis.KinesisEventPartitionProcessor",
        "method": "processRecords",
        "file": "KinesisEventPartitionProcessor.java",
        "line": 147,
        "exact": true,
        "location": "stream-driver-1.0-SNAPSHOT.jar",
        "version": "?"
      },
      {
        "class": "com.amazonaws.services.kinesis.clientlibrary.lib.worker.ProcessTask",
        "method": "callProcessRecords",
        "file": "ProcessTask.java",
        "line": 215,
        "exact": true,
        "location": "amazon-kinesis-client-1.7.4.jar",
        "version": "?"
      },
      {
        "class": "com.amazonaws.services.kinesis.clientlibrary.lib.worker.ProcessTask",
        "method": "call",
        "file": "ProcessTask.java",
        "line": 170,
        "exact": true,
        "location": "amazon-kinesis-client-1.7.4.jar",
        "version": "?"
      },
      {
        "class": "com.amazonaws.services.kinesis.clientlibrary.lib.worker.MetricsCollectingTaskDecorator",
        "method": "call",
        "file": "MetricsCollectingTaskDecorator.java",
        "line": 49,
        "exact": true,
        "location": "amazon-kinesis-client-1.7.4.jar",
        "version": "?"
      },
      {
        "class": "com.amazonaws.services.kinesis.clientlibrary.lib.worker.MetricsCollectingTaskDecorator",
        "method": "call",
        "file": "MetricsCollectingTaskDecorator.java",
        "line": 24,
        "exact": true,
        "location": "amazon-kinesis-client-1.7.4.jar",
        "version": "?"
      },
      {
        "class": "java.util.concurrent.FutureTask",
        "method": "run",
        "file": "FutureTask.java",
        "line": 266,
        "exact": true,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "java.util.concurrent.ThreadPoolExecutor",
        "method": "runWorker",
        "file": "ThreadPoolExecutor.java",
        "line": 1142,
        "exact": true,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "java.util.concurrent.ThreadPoolExecutor$Worker",
        "method": "run",
        "file": "ThreadPoolExecutor.java",
        "line": 617,
        "exact": true,
        "location": "?",
        "version": "1.8.0_111"
      },
      {
        "class": "java.lang.Thread",
        "method": "run",
        "file": "Thread.java",
        "line": 745,
        "exact": true,
        "location": "?",
        "version": "1.8.0_111"
      }
    ]
  },
  "endOfBatch": false,
  "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId": 84,
  "threadPriority": 5
}
## credit http://muhammadhamed.blogspot.com/2013/01/install-mysql-as-windows-service-from.html
@echo off
set currentPath=%~dp0
set mydump_file=%currentPath%mydb_dump-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.sql
::
:: This script is to start and configure the database server.
::
echo "Starting to insall the mysql as a service in the windows "
echo "The current path %currentPath%mysql-5.5.29-win32\ "
:: Trying to stop the service, incase of changing the installtion path.
sc stop MySQL
:: Kill the process related to the srvice.
for /f "tokens=2 delims=[:]" %%f in ('sc queryex MySQL ^|find /i "PID"') do set PID=%%f
taskkill /f /pid %PID%
:: wait 5 sec
ping 127.0.0.1 -n 10 > nul
:: Delete the service.
sc delete MySQL
:: wait 5 sec
ping 127.0.0.1 -n 10 > nul
:: Creating the service with autostart
sc create MySQL start= auto DisplayName= MySQL binPath= "%currentPath%mysql-5.5.29-win32\bin\mysqld.exe --defaults-file=\"%currentPath%my.ini\" MySQL"
call "%currentPath%mysql-5.5.29-win32\bin\mysqld.exe" --install
echo "Starting the MySQL server."
call "%currentPath%mysql-5.5.29-win32\bin\mysqld.exe"
:: Start the servie
sc start MySQL
:: wait 5 sec
ping 127.0.0.1 -n 10 > nul
:: Check if we should create or migrate
IF EXIST "%currentPath%mysql-5.5.29-win32\data\mydb" (
echo "Migrate me."
echo "dummping the database to %mydump_file% "
call "%currentPath%mysql-5.5.29-win32\bin\mysqldump.exe" -u root --password="password" mydb --default-character-set=utf8 > "%mydump_file%"
call "%currentPath%mysql-5.5.29-win32\bin\mysql.exe" -u root --password="password" --default-character-set=utf8 < "%currentPath%/migration.sql">c:/migrate.log
) ELSE (
:: Creating the schema
echo "Creating the schema."
call "%currentPath%mysql-5.5.29-win32\bin\mysql.exe" -u root --password="password" --default-character-set=utf8 < "%currentPath%/mydb.sql">c:\create.log
)
echo "Done!!"
:: echo "Press any key to exit ..."
:: pause > nul
@echo on
-- https://www.brainbell.com/tutorials/MySQL/Working_With_Cursors.htm
CREATE PROCEDURE processorders()
BEGIN
-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
-- Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
-- Declare continue handler
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000'
SET done=1;
-- Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals
(order_num INT, total DECIMAL(8,2));
-- Open the cursor
OPEN ordernumbers;
-- Loop through all rows
REPEAT
-- Get order number
FETCH ordernumbers INTO o;
-- Get the total for this order
CALL ordertotal(o, 1, t);
-- Insert order and total into ordertotals
INSERT INTO ordertotals(order_num, total)
VALUES(o, t);
-- End of loop
UNTIL done END REPEAT;
-- Close the cursor
CLOSE ordernumbers;
END;

DOWNLOAD

https://dev.mysql.com/downloads/mysql/

sudo mv ~/Downloads/mysql-5.7.17-macos10.12-x86_64 /usr/local/
sudo chmod -R 777 /usr/local/mysql-5.7.17-macos10.12-x86_64/

mkdir -p /usr/local/mysql/data/
sudo chmod -R 777 /usr/local/mysql/data/

mkdir /usr/local/mysql/share
cp /usr/local/mysql-5.7.17-macos10.12-x86_64/share/english/errmsg.sys /usr/local/mysql/share/errmsg.sys
mysql.server start

stop

sudo /usr/local/mysql/support-files/mysql.server stop
$ mysql --version
mysql  Ver 14.14 Distrib 5.7.12, for osx10.11 (x86_64) using  EditLine wrapper

Change pass

with Ui, temp password qIQ5l#YB>tZe

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql55';
Query OK, 0 rows affected (0.00 sec)

or,

ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql55';
mysqld --init-file alter-pass.sh 

https://bitbucket.org/prayagupd/e-market/src/fdd37e734eae01213e23d333299efb3ee877824e/ebazaar/?at=master

select * from user_tables;
select * from user_sequences;
[MySQL]
; Allow accessing, from PHP's perspective, local files with LOAD DATA statements
; http://php.net/mysql.allow_local_infile
mysql.allow_local_infile = On
; Allow or prevent persistent links.
; http://php.net/mysql.allow-persistent
mysql.allow_persistent = On
; If mysqlnd is used: Number of cache slots for the internal result set cache
; http://php.net/mysql.cache_size
mysql.cache_size = 2000
; Maximum number of persistent links. -1 means no limit.
; http://php.net/mysql.max-persistent
mysql.max_persistent = -1
; Maximum number of links (persistent + non-persistent). -1 means no limit.
; http://php.net/mysql.max-links
mysql.max_links = -1
; Default port number for mysql_connect(). If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order). Win32 will only look
; at MYSQL_PORT.
; http://php.net/mysql.default-port
mysql.default_port =
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket =
; Default host for mysql_connect() (doesn't apply in safe mode).
; http://php.net/mysql.default-host
mysql.default_host =
; Default user for mysql_connect() (doesn't apply in safe mode).
; http://php.net/mysql.default-user
mysql.default_user =
; Default password for mysql_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysql.default_password")
; and reveal this password! And of course, any users with read access to this
; file will be able to reveal the password as well.
; http://php.net/mysql.default-password
mysql.default_password =
; Maximum time (in seconds) for connect timeout. -1 means no limit
; http://php.net/mysql.connect-timeout
mysql.connect_timeout = 60
; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Errors will be displayed.
; http://php.net/mysql.trace-mode
mysql.trace_mode = On
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment