Академический Документы
Профессиональный Документы
Культура Документы
Aaron Zeckoski
aaronz@vt.edu
Sakai Oxford Tetra ELF Workshop
Creative Commons AttributionShareAlike 2.5 License
What is persistence?
The storage of an object on a disk or other permanent storage device or data that exists from session to session
as opposed to transient data
Persistent data typically implies that it is durable (i.e. will survive a crash or shutdown of the process) usually with some guarantee of integrity Persistence generally implies use of a database
One could use the file system (with suitably careful procedures)
Sakai in production is typically a clustered environment, and the clustering implementation is largely based around the database
Tools that will not cluster are very unlikely to be adopted into the core
The sakai.properties file is located in your sakai home in the subdirectory named sakai
this is configurable but is normally your Tomcat home
Supported databases
Only 3 databases are supported for use in Sakai
1. HSQLDB (*very* lightweight, pure-Java database, largely for testing and dev. Terrible performance characteristics storage either in memory, or a flat text file) 2. MySQL (Open source, pretty good performance, used widely in production, some very unfortunate locking issues) 3. Oracle (Costs money, pretty rock solid, works well for those that can/are prepared to afford it)
Since the majority of Sakai code uses raw SQL in some form, adding a new database is a major effort
5
HSQLDB is turned on by default, it only stores data in memory by default HSQLDB works well for development and for demos
Caveat: You cannot look at the HSQLDB database without some serious trickery
6
MySQL config
To use MySQL, uncomment the six lines under this line: ## MySQL settings Comment out the 7 lines under this one: ## HSQLDB settings Update the username and password lines to match your MySQL database
10
Spring JDBC
http://www.springframework.org/docs/refere nce/jdbc.html
Hibernate
http://www.hibernate.org/
11
JDBC Info
Java Database Connectivity Industry standard but has some issues:
The developer needs to deal with lot of plumbing and infrastructure, such as endless try-catch-finally-try-catch blocks. Applications need complex error handling to ensure that connections are properly closed after they're used, which makes the code verbose, bloated, and repetitive. JDBC uses the rather uninformative SQLException. JDBC has no exception hierarchy
12
Hibernate Info
Object / Relational mapping (ORM) and persistence / query framework
i.e. It does even more stuff for you!
From: http://www.hibernate.org/
14
Hibernate Commentary
Beyond the hype:
Hibernate *is* the best ORM persistence framework out there (probably in any language) Not to say it is without numerous issues (ORM is a *very* tricky problem and cannot actually be solved in general) Many aspects of the Hibernate framework are over-eager (lazy Collections, cascade options, long transactions) Many aspects of Hibernate are overly rigid (proxy behaviour, initial configuration sets rock-hard and cannot be changed, poor cross-ClassLoader behaviour)
Advice
Use it cautiously! Avoid lazy Collections, be conservative with cascade options In general just use it on one entity at a time, with explicit save/load on for each database operation In some (extreme) cases you may still actually want to fall back to SQL (this is actually recommended by the Hibernate team)
17
Hibernate Development
4 methods of development using Hibernate Top down (good for existing code)
implement a Java (JavaBeans) object model write a mapping document by hand, or generate it from XDoclet tags export the database tables using the Hibernate Tools
From: http://www.hibernate.org/355.html
18
Things seem to work better when not using primitives sometimes (e.g. Boolean)
19
20
22
Hibernate Tools
Hibernate provides a set of Eclipse tools
http://www.hibernate.org/255.html
Mapping Editor: An editor for Hibernate XML mapping files, supporting auto-completion and syntax highlighting Console: a view in Eclipse. Provides a tree overview of console configurations and interactive view of persistent classes and relationships. Also allows the execution of HQL queries against your database and browsing of results in Eclipse. Development Wizards: Includes the Hibernate configuration (cfg.xml) files wizard and reverse engineering wizard for turning an existing database schema into POJO source files and HBM files.
From: http://www.hibernate.org/255.html
23
Hibernate in Sakai
3 ways of using Hibernate in Sakai
1. Create a SessionFactory using settings inside your tool 2. Create a SessionFactory from the global Sakai sessionFactoryBase 3. Add our HBMs to the global Sakai sessionFactory
24
Method 1
Create a Hibernate SessionFactory using config settings in your tool
You should use this when connecting to an external database Do not use this method to connect to the internal Sakai database! More info on session configuration:
http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html
25
Method 2
Create a SessionFactory from the global Sakai SessionFactoryBase
This is not the recommended method but if you are using a single Maven project for your app then you have to use it
Method 3
Add our HBMs to the global Sakai SessionFactory using AddableSessionFactoryBean
This is the preferred method Works best for all but the simplest apps Requires the tool to deploy portions to shared and components so it cannot be used for simple tools
Demonstrated in tasklist
From: http://bugs.sakaiproject.org/confluence/display/BOOT/Using+the+Sakai+global+sessionFactory
27
More on GenericDao
Get the code and Javadocs from the VT Maven repository:
http://source.edtech.vt.edu/maven/generic-dao/
29
Update project.xml
Add the Hibernate dependency to the tool/project.xml file Note that we use 3 property variables from master/project.properties
<dependency> <groupId>${sakai.hibernate.groupId}</groupId> <artifactId>${sakai.hibernate.artifactId}</artifactId> <version>${sakai.hibernate.version}</version> </dependency>
31
33
34
Template customization
Change the class name and table name
org.sakaiproject.toolname.model.MyObject
Change the id sequence name Copy and paste the property block to add the properties from your persistent object
owner siteId creationDate
Etc
35
Extend HibernateDaoSupport
part of Spring-Hibernate
Spring configuration
Now we need to tie everything together with Spring First we will tell hibernate about our MYObject.hbm.xml mapping file Next we will give the hibernate stuff to our DAO implementation Finally we will tie the new DAO to the rest of the webapp
37
This allows us to use our persistent objects with the Sakai Global SessionFactory
38
This connects the new DAO to Hibernate The DAO implementation should extend HibernateDaoSupport
39
If your operations are not in a transaction you will see many problems, especially in MySQL This involves much less work than opening and closing transactions in code, and is more reliable
40
Any questions?
Check out the section on persistence in the Programmers Caf for more information
http://bugs.sakaiproject.org/confluence/display/BOOT/Persistence
41