27
MySQL/JVM Kevin Tankersley A Framework for Enabling Java Language Stored Procedures in MySQL

MySQL/JVM

Embed Size (px)

DESCRIPTION

A presentation I gave to summarize the project work that I did for my Masters

Citation preview

Page 1: MySQL/JVM

MySQL/JVMMySQL/JVM

Kevin TankersleyKevin Tankersley

A Framework for Enabling Java Language Stored Procedures in MySQL

A Framework for Enabling Java Language Stored Procedures in MySQL

Page 2: MySQL/JVM

External Language RoutinesExternal Language Routines

• Stored procedure languages typically specialized for data access

• The available function libraries typically focus on numeric, date, time, and string functions

• How to handle more unusual situations?

Page 3: MySQL/JVM

Sample ProblemsSample Problems

• XML validation, XPath querying, XSL transformations

• Network Access and Consuming web services

• Encryption and signature algorithms

Page 4: MySQL/JVM

Vendor SolutionsVendor Solutions

• Oracle allows stored procedures to be written in Java

• Microsoft SQL Server allows stored procedures to be written in .NET

• PostgreSQL allows procedures to be written in several external languages, including Java

• No solution for MySQL, though…

Page 5: MySQL/JVM

The ProjectThe Project

• Add support to MySQL for creating Java stored procedures

• Use MySQL as the database because it is open source and widely used

• Use Java as the language because it is multi-platform and widely known

• Design for compliance with ISO standards

• ISO 9075-13 Defines the syntax and behavior of Java routines in the DB

Page 6: MySQL/JVM

DemonstrationDemonstration

Page 7: MySQL/JVM

Design OverviewDesign Overview

• Four major tasks to accomplish:

Link the project to the Java Virtual Machine

o Modify the MySQL language syntax to accommodate Java routines

o Modify classes, tables, and support libraries to recognize Java routines

o Build a framework for loading classes and invoking methods at runtime

Page 8: MySQL/JVM

Java Virtual Machine LinkageJava Virtual Machine Linkage

• Java Native Interface

• Platform for making native calls between Java runtime and C/C++

• Supports callbacks from Java code, or JVM invocation from C/C++

• Requires the shared library jvm.dll and the header jni.h at compile time

• Requires the full Java Runtime Environment on the path at run time

Page 9: MySQL/JVM

JVM Linkage ExampleJVM Linkage Example

Page 10: MySQL/JVM

JVM Thread Linkage ExampleJVM Thread Linkage Example

Page 11: MySQL/JVM

Design OverviewDesign Overview

• Four major tasks to accomplish:

Link the project to the Java Virtual Machine

Modify the MySQL language syntax to accommodate Java routines

o Modify classes, tables, and support libraries to recognize Java routines

o Build a framework for loading classes and invoking methods at runtime

Page 12: MySQL/JVM

Language Changes: ParsingLanguage Changes: Parsing

• Statement Parsing

• MySQL uses the GNU Bison parser

• Parser reads user query and creates a parse tree descriptor of type LEX*

• Server libraries use the classes and data in LEX* to carry out the request

• Current stored procedure language compliant with ISO standard

Page 13: MySQL/JVM

Language Changes: CharacteristicsLanguage Changes: Characteristics

Page 14: MySQL/JVM

Design OverviewDesign Overview

• Four major tasks to accomplish:

Link the project to the Java Virtual Machine

Modify the MySQL language syntax to accommodate Java routines

Modify classes, tables, and support libraries to recognize Java routines

o Build a framework for loading classes and invoking methods at runtime

Page 15: MySQL/JVM

Stored Procedure Changes: LifecycleStored Procedure Changes: Lifecycle

• Lifecycle of a stored procedure

1. User sends definition to server

2. Server parses definition and creates an object of type sp_head in LEX*

3. The sp_head is cached, and the definition is stored in mysql.proc

4. User calls the procedure

5. Server retrieves sp_head from cache, or reconstructs it

6. The execute function of the sp_head is invoked

Page 16: MySQL/JVM

Stored Procedure Changes: ModificationsStored Procedure Changes: Modifications

• Add columns to mysql.proc

• Alter procedure definition code

• Alter class sp_head

• Change structure st_chistics

• Change execution functions to recognize Java routines

Page 17: MySQL/JVM

Design OverviewDesign Overview

• Four major tasks to accomplish:

Link the project to the Java Virtual Machine

Modify the MySQL language syntax to accommodate Java routines

Modify classes, tables, and support libraries to recognize Java routines

Build a framework for loading classes and invoking methods at runtime

Page 18: MySQL/JVM

Method Invocation: OverviewMethod Invocation: Overview

• Necessary Subsystems:

• A system for storing compiled Java code

• A classloading mechanism to locate and define classes at runtime

• A parameter translation API to convert MySQL data types to JVM data types

• A method invocation API to call the relevant class method

Page 19: MySQL/JVM

Method Invocation: BytecodeMethod Invocation: Bytecode

• Best place for the bytecode is in the database itself• More secure: No need to give

developers file system access• More manageable: Easier for

developers and DBAs to track installed libraries

• More portable: Paths, environment variables, file permissions vary between platforms

• New Tables• Mysql.jclass: Class files• Mysql.jmethod: Method

descriptions• Mysql.jresource: Other resources

Page 20: MySQL/JVM

Method Invocation: Class LoadingMethod Invocation: Class Loading

• Standard loaders only know how to find classes on the file system• Create a custom class loader that

knows how to get classes from DB

• Need to link custom loader to DB

• Using JDBC is problematic• Solution: Use JNI Callbacks• Create a server function get_jclass

to access mysql.jclass through native table handlers

• Use a native method in the class loader

• Use JNI to link the native method with the get_jclass function at run time

Page 21: MySQL/JVM

Method Invocation: ParametersMethod Invocation: Parameters

• MySQL and JVM use different data types

• Created JParam API to manage conversions between MySQL types and JVM types

• Numerics:• Need to map MySQL types to Java

types of equal or greater width (e.g. TINYINT to byte)

• Runtime checks for unsigned overflow

• Strings:• MySQL: Support for many

encodings• Java: Uses only UTF-16 Unicode

Page 22: MySQL/JVM

Method Invocation: Invocation APIMethod Invocation: Invocation API

• Created class MyJThread to tie class loading, parameters, and invocation together

• When Created:• Attaches to JVM• Locates bytecode for class loader• Defines class loader

• When Called:• Parse method signature• Locate bytecode for entry class• Define entry class with class loader• Translate parameters to JVM types• Invoke method on entry class• Save return value in MySQL context

Page 23: MySQL/JVM

Project SummaryProject Summary

• Successfully added all elements necessary to create Java routines in MySQL

• Framework to link MySQL to JVM

• Standards-compliant changes to the language

• Modifications to existing elements of stored procedure execution

• New classes to support native class loading, parameter translation, and method invocation

Page 24: MySQL/JVM

Future WorkFuture Work

• Additional features to add:

• Role-based security model for JVM resources

• Fully native JDBC Driver

• New server configuration variables

• A bytecode cache

• User-defined types

• More data type translations

Page 25: MySQL/JVM

ReferencesReferences

Page 26: MySQL/JVM

ReferencesReferences

Page 27: MySQL/JVM

QuestionsQuestions