Persistence – Iteration 4 Vancouver Bootcamp Aaron Zeckoski aaronz@vt.edu
What is persistence? Taking temporary data (like in-memory program data) and storing it in a more permanent form Putting program data somewhere so you can retrieve it later (e.g. after a restart) Writing the information in your Java objects somewhere so you can get to it again after they are destroyed
How do we persist data in Sakai? Pretty much always use the database Some file system storage for tools like resources so data is accessible via WebDav You should probably use the database
# DATABASE CONFIGURATION Databases and Sakai Settings to control the database that is used are stored in sakai.properties The sakai.properties file is located in your sakai home (this is configurable but is normally your tomcat home) in the sakai directory Look for the following line in the file # DATABASE CONFIGURATION
Some DB config tips Always leave auto.dll=true HSQLDB is turned on by default, it only stores data in memory by default HSQLDB works well for development and for demos You cannot look at the HSQLDB database without some serious trickery
More DB config tips MySQL is easy to setup and use and runs pretty efficiently Allows you to look at the database to see if things are working Seems to have trouble working with Sakai in Windows sometimes If all else fails, switch to HSQLDB file storage
HSQLDB file storage To use HSQLDB in file mode (where it stores data on the filesystem), comment out this line: url@javax.sql.BaseDataSource=jdbc:hsqldb:. and uncomment this one url@javax.sql.BaseDataSource= jdbc:hsqldb:${sakai.home}/db/sakai.db
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
One last DB tip You can turn on verbose Hibernate logging in the sakai.properties file Change the following from false to true # enable hibernate SQL debugging output hibernate.show_sql=false
3 ways to persist data to the DB JDBC http://java.sun.com/products/jdbc/ Spring JDBC http://www.springframework.org/docs/reference/jdbc.html Hibernate http://www.hibernate.org/
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 From: http://java.sun.com/products/jdbc/
Spring JDBC Info Abstraction framework for JDBC i.e. It does lots of stuff for you! Some features of Spring JDBC JdbcDaoSupport – super class, provides JdbcTemplate access Spring provides an abstract exception layer, moving verbose and error-prone exception handling out of application code into the framework. The framework takes care of all exception handling; application code can concentrate on extracting results by using appropriate SQL. Spring provides a significant exception hierarchy for your application code to work with in place of SQLException. For creating instances of oracle.sql.BLOB (binary large object) and oracle.sql.CLOB(character large object), Spring provides the class org.springframework.jdbc.support.lob.OracleLobHandler. From: http://www.springframework.org/docs/reference/jdbc.html
Hibernate Info Object / Relational mapping (ORM) and persistence / query framework i.e. It does even more stuff for you! Some features of Hibernate HibernateDaoSupport – super class, easy HibernateTemplate access Database independence - sits between the database and your java code, easy database switch without changing any code Object / Relational Mapping (ORM) - Allows a developer to treat a database like a collection of Java objects Object oriented query language (HQL) - Fully supports polymorphic queries, native SQL, and Criteria queries Hibernate Mapping - Uses HBM XML files to map value objects (POJOs) to database tables Transparent persistence - Allows easy saves/delete/retrieve for simple value objects High performance because it is such a lightweight framework From: http://www.hibernate.org/
More Hibernate Info Hibernate basically sits between the DB and your code Can map persistent objects to tables In Sakai, the Hibernate configuration is set for you already From: http://www.hibernate.org/hib_docs/v3/reference/en/html/architecture.html
Even more Hibernate Info Hibernate 2-tier web architecture Can send data to JDBC or XML files Best to just use it the way Sakai does (JDBC) From: http://www.hibernate.org/354.html
Hibernate Development 4 methods of development using Hibernate Top down (good for existing code, we will use it for this exercise) implement a Java (JavaBeans) object model write a mapping document by hand, or generate it from XDoclet tags export the database tables using the hbm2ddl (SchemaExport) tool Bottom up (good for existing database or code conversion) start with an existing data model use the Hibernate Tools to generate the mapping documents use the hbm2java (CodeGenerator) tool to generate skeletal Java code fill in the business logic by hand Middle out (good for new development) express your conceptual object model directly as a mapping document use the hbm2java tool to generate skeletal Java code export the database tables using the hbm2ddl tool Meet in the middle (good for existing JDBC to Hibernate switch) start with an existing data model and existing Java classes write a mapping document to adapt between the two models From: http://www.hibernate.org/355.html
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. We will work without the tools for this exercise From: http://www.hibernate.org/255.html
Other Hibernate Tools Hibernate Synchronizer http://hibernatesynch.sourceforge.net/ Actively developed plugin for Eclipse Works with Hibernate 3 (so does Sakai 2.2!) Can generate value objects, subclasses, components, and DAOs from HBM files Works well for new large scale code projects Use Hibernate Tools for smaller projects From: http://hibernatesynch.sourceforge.net/
Hibernate in Sakai 3 ways of using Hibernate in Sakai Create a SessionFactory using settings inside your tool Create a session factory from the global Sakai sessionFactoryBase Add our HBMs to the global Sakai sessionFactory From: http://bugs.sakaiproject.org/confluence/display/BOOT/Hibernate+in+Sakai
Method 1 Create a Hibernate SessionFactory using config settings in your tool You should use this when connecting to an external database Don’t 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
Method 2 Create a session factory from the global Sakai SessionFactoryBase We will use this method for the workshop and the Task List Tool Exercise This method works well for simple tools More complex tools should use method 3 Making a connection this way does not always work on Windows with MySQL From: http://bugs.sakaiproject.org/confluence/display/BOOT/Creating+sessions+from+the+Sakai+SessionFactoryBase
Method 3 Add our HBMs to the global Sakai sessionFactory This is the preferred method Works best for more complex applications Requires the tool to deploy portions to shared and components so cannot be used for simple tools Demonstrated in tasklist-complex From: http://bugs.sakaiproject.org/confluence/display/BOOT/Using+the+Sakai+global+sessionFactory
Let’s write some code! Should have iteration3 code complete If not, check out the iteration3 code: https://source.sakaiproject.org/contrib/programmerscafe/tags/sakai-2.2/tasklist-iteration3-CSS/