Configuring your OpenXava 6.x (or
older) application to go against MySQL is very simple, basically you have
to install the JDBC driver for MySQL and define correctly the datasource.
You don't need to touch any code of your application.
We assume you have already
installed and running MySQL.
Download the JDBC driver for MySQL
Download the MySQL driver from here: https://dev.mysql.com/downloads/connector/j/
You will download a file like this:
mysql-connector-java-8.0.17.zip
(the version numbers may vary). Uncompress it to find inside a file called
mysql-connector-java-8.0.17.jar (or so), this last file, the .jar,
is the JDBC controller we're going to use.
Create a classpath variable in Eclipse
In order you can connect to MySQL from Eclipse we're going to declare a
classpath variable that points to the MySQL JDBC driver, so you can use it
in any project you need easily. For that, in Eclipse go to Window >
Preferences > Java > Build Path > Classpath Variables where
you can add the new variable:
You can call the variable
MYSQL_DRIVER instead of DB_DRIVER if your prefer. The path is the path of
the JDBC driver, in our case the path of mysql-connector-java-8.0.17.jar
we have just downloaded.
Add the DB_DRIVER variable to your Eclipse project
In the project you're going to use MySQL you have to add the variable
declared above. Click with right mouse button on your project and then
choose Java Build Path > Configure Build Path...:
Then select the Libraries
tab:
With this we have the driver
available for the development environment.
Add the JDBC driver to the production Tomcat
Adding
the driver in production is much easier. Copy mysql-connector-java-8.0.17.jar
to the lib folder of your Tomcat. Done.
Adjust your datasource definition
For development edit web/META-INF/context.xml of your Eclipse
project, and for production edit conf/context.xml of your Tomcat
to adjust the datasource to point to MySQL, something like this:
<Resource name="jdbc/MyAppDS" auth="Container"
type="javax.sql.DataSource"
maxTotal="100" maxIdle="20" maxWaitMillis="10000"
username="root" password="ao49fmsk"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/"/>
The differences are the driverClassName
and the url. Obviously, instead of localhost you should
put the address of the server that hosts MySQL, and also put the correct username
and password.
Update persistence.xml
In the
default persistence unit of
persistence.xml (in
persistence/META-INF)
you have to change the
hibernate.default_schema property by
hibernate.default_catalog.
Also, if you use
hibernate.dialect property just remove it.
<persistence-unit name="default">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java://comp/env/jdbc/MyAppDS</non-jta-data-source>
<class>org.openxava.session.GalleryImage</class>
<class>org.openxava.web.editors.DiscussionComment</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="update"/>
<property name="javax.persistence.create-database-schemas" value="true"/>
<property name="hibernate.default_catalog" value="myappdb"/>
<property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
</properties>
</persistence-unit>
Note as we use
hibernate.default_catalog to indicate your MySQL
database,
myappdb in this case.
Morever, you have to modify the
junit
persistence unit to point to MySQL.
<!-- JUnit MySQL -->
<persistence-unit name="junit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.openxava.web.editors.DiscussionComment</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="ao49fmsk"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/myappdb"/>
</properties>
</persistence-unit>
Adapt the username, password
and url to your MySQL configuration.