Upload
justanotherabstraction
View
1.184
Download
1
Tags:
Embed Size (px)
DESCRIPTION
A presentation I gave to summarize the project work that I did for my Masters
Citation preview
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
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?
Sample ProblemsSample Problems
• XML validation, XPath querying, XSL transformations
• Network Access and Consuming web services
• Encryption and signature algorithms
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…
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
DemonstrationDemonstration
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
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
JVM Linkage ExampleJVM Linkage Example
JVM Thread Linkage ExampleJVM Thread Linkage Example
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
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
Language Changes: CharacteristicsLanguage Changes: Characteristics
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
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
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
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
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
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
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
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
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
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
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
ReferencesReferences
ReferencesReferences
QuestionsQuestions