Wednesday, December 14, 2011

Persistence

I wouldn't normally think of putting persistence this early in the discussion, but you can't really do much if you can't persist your data.  So we will start with persistence and go from there.  When we discuss persistence we are automatically talking about several different issues all bundled together.  And, unfortunately, one doesn't work without the other.  If you look on the web for a persistence example of something, you will invariably find an example that will discuss your issue but use a different environment.  And, unless you understand the difference, it will be difficult to separate your issue from their example.  Although I will give you a specific example and therefor suffer the same environment issue, I hope to give you enough data that you can get your own environment working.

First, my environment:
     Database: MySQL 14.14 distribution 5.5.17 for Win 32 (x86).
     JPA provider: Hibernate 3.2.5
     JTA and JNDI provider: Glassfish 3.1
     Database Administrator: MySQL Workbench  5.2CE

What are the parameters we will configure our environment for:

     I decided against using anything that was Hibernate specific in the code and to run a pure JPA environment, so I got rid of the hibernate.cfg.xml and replaced it with the persistence.xml file.  Next, I wanted as much as possible to be specific to the project and not to the container (Glassfish) that we are running in, so only a couple of things need to be done outside the scope of the code and configuration files.

With that said, you will need to create a database user, give the user privileges, create a connection pool, and create a data source in your JEE server.  The code examples assume certain names, you can either use these names in your set up, or modify the code to fit your environment.  The example below assumes you wish to use the names in the code.

The environment we need:
   Database (or schema) name: jsfdemodb
   User name : JSFDemoUser
   Password : ItWorks!
   JNDI name : JSFDemoJNDI
   Data Source: JSFDemoPU
   Privileges : JSFDemoUser needs to be able to create, drop, and modify tables.

The files associated with this environment setup:  persistence.xml, glassfish-resources.xml.  Note: the glassfish-resources.xml file is specific to the JEE server setup, if you are running a different server, you will need to configure the server to have a connection pool and datasource referenced with the JNDI name of JSFDemoJNDI to tie it back to the persistence.xml file.  For an example a JBoss configuration, see Project 2.

To configure Glassfish, see Container Configuration and follow the instructions for the Glassfish container.

Create the database
1. Open MySQL WorkBench
2. Click 'Open Connection to Start Querying' / 'Local instance: MySQL' or whatever database you have setup.
3. Click on the Database icon to 'Create a New Schema '.
4. Fill in 'Name' as jsfdemodb
5. Click 'Apply'
6. Click 'Apply'
You should now have the schema created.

Create the database user:
1. Go to the Home Tab if WorkBench is still open, or Open MySQL WorkBench.
2. Click 'Server Administration' / 'Local: MySQL' or whatever database you have setup.
3. Login as root.  The default password is: password.
4. Select Security/Users and Privileges.
5. Click 'Add Account'
6. Fill in 'Login Name' with 'JSFDemoUser'
7. Fill in 'Limit Connectivity to Hosts Matching' with '%'.  Note: this will allow this user to connect from any machine, if you do not wish this, change the % to what you want.
8. Fill in 'Password' and 'Confirm Password'  with ItWorks!
9. Select the 'Schema Privileges' tab.
10. Select 'JSFDemoUser'
11. Click on 'Add Entry'
12. Under Schema, select the radio button 'Selected Schema' and select 'JSFDemo '
13. Click 'OK'

14. Click on 'Select All'
15. Click on 'Save Changes'
You should now have the user created and privileges assigned.

There are many configuration options, and methods that have not been discussed here.  (i.e. All the database configuration could have been done with the mysql command utility and SQL statements.  Also, you can restrict the host the JSFDemoUser can access the database from.  etc.)  But those decisions are best made by your specific requirements and are left for you to ponder.

No comments:

Post a Comment