@@ -21,13 +21,73 @@ A relational database (MariaDB) that holds your entities (we use Liquibase for g
2121
2222### Minimal Example
2323
24+ #### Persistence.xml
25+
26+ Of course you can use your own. This example is only given as a reference and quick-start support.
27+
28+ ``` xml
29+ <?xml version =" 1.0" encoding =" UTF-8" ?>
30+ <persistence version =" 2.0"
31+ xmlns=" http://java.sun.com/xml/ns/persistence" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
32+ xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence
33+ http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" >
34+
35+ <persistence-unit name =" my-persistence-unit" transaction-type =" RESOURCE_LOCAL" >
36+ <!-- Hibernate specific -->
37+ <provider >org.hibernate.jpa.HibernatePersistenceProvider</provider >
38+
39+ <class >info.unterrainer.commons.rdbutils.entities.BasicJpa</class >
40+ <class >info.unterrainer.commons.rdbutils.converters.LocalDateTimeConverter</class >
41+
42+ <properties >
43+ <!-- Hibernate-specific / MariaDB-JDBC-driver specific -->
44+ <property name =" javax.persistence.jdbc.driver" value =" org.mariadb.jdbc.Driver" />
45+ <property name =" javax.persistence.lock.timeout" value =" 10000" />
46+ <property name =" javax.persistence.query.timeout" value =" 60000" />
47+ <property name =" hibernate.connection.driver_class" value =" org.mariadb.jdbc.Driver" />
48+ <property name =" hibernate.dialect" value =" org.hibernate.dialect.MariaDBDialect" />
49+ <property name =" hibernate.temp.use_jdbc_metadata_defaults" value =" false" />
50+ <property name =" hibernate.jdbc.time_zone" value =" UTC" />
51+
52+ <property name =" hibernate.show_sql" value =" false" />
53+ <property name =" hibernate.format_sql" value =" false" />
54+ <property name =" hibernate.c3p0.min_size" value =" 5" />
55+ <property name =" hibernate.c3p0.max_size" value =" 200" />
56+ <!-- Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. -->
57+ <property name =" hibernate.c3p0.timeout" value =" 300" />
58+ <!-- The size of c3p0’s global PreparedStatement cache over all connections. Zero means statement caching is turned off. -->
59+ <property name =" hibernate.c3p0.max_statements" value =" 500" />
60+ <!-- If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds. -->
61+ <property name =" hibernate.c3p0.idle_test_period" value =" 3000" />
62+ <!-- Is set to true, the connection is tested with a simple query before being returned to a user -->
63+ <property name =" hibernate.c3p0.testConnectionOnCheckout" value =" true" />
64+ <property name =" hibernate.c3p0.statementCacheNumDeferredCloseThreads" value =" 1" />
65+
66+ </properties >
67+ </persistence-unit >
68+ </persistence >
69+ ```
70+
71+ #### Code
72+
2473``` java
25- // Get configuration containing necessary environment variables.
26- configuration = MyProgramConfiguration . read();
74+ // To get the necessary configuration values, we use environment variables.
75+ // You can see the necessary fields in the java-rdb-utils project (configuration).
76+ // Those currently are:
77+ //
78+ // DB_SERVER the IP or URI of your server
79+ // DB_PORT
80+ // DB_NAME
81+ // DB_USER
82+ // DB_PASSWORD
83+
2784// Create an EntityManagerFactory using java-rdb-utils.
2885// Registers shutdownhook to close emf as well.
86+ // This method gets the connection data from the environment variables
87+ // as stated above.
88+ // "my-server" is the persistence-unit-name
2989EntityManagerFactory emf =
30- dbUtils. createAutoclosingEntityManagerFactory(MyProgram . class, " my-server " );
90+ dbUtils. createAutoclosingEntityManagerFactory(MyProgram . class, " my-persistence-unit " );
3191
3292// Create the server.
3393HttpServer server = HttpServer . builder()
0 commit comments