User Tools

Site Tools


java:java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

java:java [2022/07/26 06:06] – [Hibernate connection pooling and mysql connection timeout] adminjava:java [2022/10/29 16:15] (current) – external edit 127.0.0.1
Line 562: Line 562:
   * **Atomic variables** have features that minimize synchronization and help avoid memory consistency errors.   * **Atomic variables** have features that minimize synchronization and help avoid memory consistency errors.
   * **ThreadLocalRandom** (in JDK 7) provides **efficient generation of pseudorandom numbers from multiple threads**.   * **ThreadLocalRandom** (in JDK 7) provides **efficient generation of pseudorandom numbers from multiple threads**.
-=== Executor Interfaces === + 
-=== Thread pools ===+==== Thread pools ===
 +{{:java:thread-pool-tasks.png|}}
 refer:  refer: 
-  * https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html +  * https://www.baeldung.com/thread-pool-java-and-guava
-  * http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html+
   * http://howtodoinjava.com/2012/10/20/how-to-use-blockingqueue-and-threadpoolexecutor-in-java   * http://howtodoinjava.com/2012/10/20/how-to-use-blockingqueue-and-threadpoolexecutor-in-java
 +  * http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
 Thread pools address two different problems:  Thread pools address two different problems: 
   * They usually provide improved performance when **executing large numbers of asynchronous tasks**, due to reduced per-task invocation overhead   * They usually provide improved performance when **executing large numbers of asynchronous tasks**, due to reduced per-task invocation overhead
Line 1202: Line 1203:
 session.close(); session.close();
 </code> </code>
-==== Hibernate connection pooling and mysql connection timeout ====+ 
 +=== Custom Configure === 
 +refer: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional 
 +=== Hibernate map editor === 
 +=== Hibernate reverseengineering tool === 
 +refer:  
 +  * http://docs.jboss.org/tools/4.0.1.Final/en/hibernatetools/html_single/#refeng_codegen 
 +  * http://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html 
 +  * http://docs.jboss.org/tools/latest/en/hibernatetools/html/plugins.html 
 +  * http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/ 
 +Steps by steps to use reverseengineering tool in eclipse: 
 +  - Step1:** Create Hibernate configuration file(hibernate.cfg.xml)** for project: **right click project name -> New -> Hibernate Configuration File(.cfg.xml)** {{:java:create-hibernate-config.jpg|}}\\ with some basic informations below: 
 +    * database name: test 
 +    * username: admin 
 +    * password: admin12!@  
 +  - Step2: **Create Hibernate Console Configuration**: A Console configuration describes how the Hibernate plugin should configure Hibernate and what configuration files and classpaths are needed to load the POJO's, JDBC drivers etc. It is **required** to make use of **query prototyping, reverse engineering and code generation** 
 +  - Step3: view **Hibernate Console Configuration** created from Step2. Go to **window->show view->Hibernate Configurations**\\ {{:java:hibernate-console-configuration.jpg|}} 
 +  - Step4: Create configuration for Hibernate code generator {{:java:hibernate_code_gen.png|}} 
 +  - Step5: Config Main for Hibernate code generator 
 +    * Check option **Reverse engineer from JDBC Connection** 
 +    * And enter package name, for example: **vn.casino.entities**{{:java:hibernate-code-generator-main.jpg|}}  
 +  - Step6: Config Export for **Hibernate code generator**: Check 2 options **Domain Code and Hibernate XML Mappings**: {{:java:hibernate-code-generator-exporter.jpg|}}Note: You add option **Hibernate XML Configuration** to generate **hibernate.cfg.xml** 
 +  - Step7: Click **Run** to generate java code and Hibernate XML Mappings 
 +==== Hibernate Foreign key Using XML mapping ==== 
 +refer:  
 +  * one-to-one: http://www.tutorialspoint.com/hibernate/hibernate_one_to_one_mapping.htm 
 +  * one-to-many: http://www.tutorialspoint.com/hibernate/hibernate_one_to_many_mapping.htm 
 +==== Hibernate log ==== 
 +You need to config display log for package org.hibernate with log4j to check hibernate log<code ini> 
 +# org.hibernate FileAppender 
 +log4j.appender.hibernateAppender=org.apache.log4j.DailyRollingFileAppender 
 +log4j.appender.hibernateAppender.layout=org.apache.log4j.PatternLayout 
 +log4j.appender.hibernateAppender.File=logs/cardgameExt/hibernate.log 
 +log4j.appender.hibernateAppender.layout.ConversionPattern=%d{dd MMM yyyy | HH:mm:ss,SSS} | %-5p | %t | %c{3} | %3x | %m%n 
 +log4j.appender.hibernateAppender.Encoding=UTF-8 
 +log4j.appender.hibernateAppender.DatePattern='.'yyyy-MM-dd 
 +log4j.category.org.hibernate=INFO,consoleAppender,hibernateAppender 
 +</code> 
 +===== Java Connection Pool ====
 Connection pooling is the technique used to increase the performance of the application when an  Connection pooling is the technique used to increase the performance of the application when an 
 Connection pooling takes care of **managing connections(prepare,open and close)** Connection pooling takes care of **managing connections(prepare,open and close)**
Line 1209: Line 1248:
   * If a connection is **stale**, the pooling mechanism would then **close it and re-open a new one**.   * If a connection is **stale**, the pooling mechanism would then **close it and re-open a new one**.
   * This way we can use the connections in a more better way, as **we no need to wait for the connection to be established during the actual execution of our code** and we don’t have to worry about stale connections   * This way we can use the connections in a more better way, as **we no need to wait for the connection to be established during the actual execution of our code** and we don’t have to worry about stale connections
-  * org.hibernate.connection Interface ConnectionProvider<code java> 
-public interface ConnectionProvider 
-A strategy for obtaining JDBC connections.  
- 
-Implementors might also implement connection pooling. 
- 
-The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections. 
- void close()  
-          Release all resources held by this provider. 
- void closeConnection(Connection conn)  
-          Dispose of a used connection. 
- void configure(Properties props)  
-          Initialize the connection provider from given properties. 
- Connection getConnection()  
-          Grab a connection, with the autocommit mode specified by hibernate.connection.autocommit. 
- boolean supportsAggressiveRelease()  
-          Does this connection provider support aggressive release of JDBC connections and re-acquistion of those connections (if need be) later? 
-This is used in conjunction with org.hibernate.cfg.Environment.RELEASE_CONNECTIONS to aggressively release JDBC connections. 
-</code> 
 refer:  refer: 
-  * https://www.progress.com/tutorials/jdbc/jdbc-jdbc-connection-pooling +  * https://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html
-  * http://stackoverflow.com/questions/10695028/hibernate-mysql-connection-timeout +
-  * https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch01.html +
-  * {{:java:pooling.jpg|}}+
   * {{:java:connection-pool.png|}}   * {{:java:connection-pool.png|}}
-Below are basic steps config for connections in Hibernate(each connection in MySQL was a session in Hibernate):+  * {{:java:pooling.jpg|}} 
 + 
 +==== Default Hibernate connection pooling ==== 
 +Below are basic steps config for connections in Hibernate(each connection in MySQL was a session in Hibernate)(refer: https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/configuration-hibernatejdbc.html):
   - Config max connections in MySQL:   - Config max connections in MySQL:
     * config temporary in MySQL Admin console:<code sql>     * config temporary in MySQL Admin console:<code sql>
Line 1266: Line 1286:
 } }
 </code> </code>
-=== Default JDBC Connection and Default Connection Pool=== 
 Most important Hibernate JDBC properties:<code xml> Most important Hibernate JDBC properties:<code xml>
 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
Line 1275: Line 1294:
 <property name="hibernate.connection.pool_size">1</property> <property name="hibernate.connection.pool_size">1</property>
 </code>=> Hibernate's internal connection pooling algorithm is rudimentary, and is provided for development and testing purposes. **Use a third-party pool for best performance and stability**. To use a third-party pool, **replace the hibernate.connection.pool_size property** with **settings specific to your connection pool of choice**. This disables Hibernate's internal connection pool. </code>=> Hibernate's internal connection pooling algorithm is rudimentary, and is provided for development and testing purposes. **Use a third-party pool for best performance and stability**. To use a third-party pool, **replace the hibernate.connection.pool_size property** with **settings specific to your connection pool of choice**. This disables Hibernate's internal connection pool.
-=== org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool ===+==== org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool ====
 refer: refer:
   * https://www.mchange.com/projects/c3p0/ and github https://github.com/swaldman/c3p0   * https://www.mchange.com/projects/c3p0/ and github https://github.com/swaldman/c3p0
   * Configuration c3p0: http://www.mchange.com/projects/c3p0/#configuration   * Configuration c3p0: http://www.mchange.com/projects/c3p0/#configuration
 +  * org.hibernate.connection Interface ConnectionProvider<code java>
 +public interface ConnectionProvider
 +A strategy for obtaining JDBC connections. 
 +
 +Implementors might also implement connection pooling.
 +
 +The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections.
 + void close() 
 +          Release all resources held by this provider.
 + void closeConnection(Connection conn) 
 +          Dispose of a used connection.
 + void configure(Properties props) 
 +          Initialize the connection provider from given properties.
 + Connection getConnection() 
 +          Grab a connection, with the autocommit mode specified by hibernate.connection.autocommit.
 + boolean supportsAggressiveRelease() 
 +          Does this connection provider support aggressive release of JDBC connections and re-acquistion of those connections (if need be) later?
 +This is used in conjunction with org.hibernate.cfg.Environment.RELEASE_CONNECTIONS to aggressively release JDBC connections.
 +</code>
   * org.hibernate.connection.C3P0ConnectionProvider https://github.com/hibernate/hibernate-orm/tree/741b6b71f1552aa224fb61d38b85b32e0b8a19b4/hibernate-c3p0 -> Integration for c3p0 Connection pooling into Hibernate ORM<code java>   * org.hibernate.connection.C3P0ConnectionProvider https://github.com/hibernate/hibernate-orm/tree/741b6b71f1552aa224fb61d38b85b32e0b8a19b4/hibernate-c3p0 -> Integration for c3p0 Connection pooling into Hibernate ORM<code java>
 import javax.sql.DataSource; import javax.sql.DataSource;
Line 1325: Line 1363:
   * Global statements pool(statements cached): <code xml>   * Global statements pool(statements cached): <code xml>
 <property name="hibernate.c3p0.max_statements">1000</property> <property name="hibernate.c3p0.max_statements">1000</property>
-</code> 
-=== Custom Configure === 
-refer: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional 
-=== Hibernate map editor === 
-=== Hibernate reverseengineering tool === 
-refer:  
-  * http://docs.jboss.org/tools/4.0.1.Final/en/hibernatetools/html_single/#refeng_codegen 
-  * http://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html 
-  * http://docs.jboss.org/tools/latest/en/hibernatetools/html/plugins.html 
-  * http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/ 
-Steps by steps to use reverseengineering tool in eclipse: 
-  - Step1:** Create Hibernate configuration file(hibernate.cfg.xml)** for project: **right click project name -> New -> Hibernate Configuration File(.cfg.xml)** {{:java:create-hibernate-config.jpg|}}\\ with some basic informations below: 
-    * database name: test 
-    * username: admin 
-    * password: admin12!@  
-  - Step2: **Create Hibernate Console Configuration**: A Console configuration describes how the Hibernate plugin should configure Hibernate and what configuration files and classpaths are needed to load the POJO's, JDBC drivers etc. It is **required** to make use of **query prototyping, reverse engineering and code generation** 
-  - Step3: view **Hibernate Console Configuration** created from Step2. Go to **window->show view->Hibernate Configurations**\\ {{:java:hibernate-console-configuration.jpg|}} 
-  - Step4: Create configuration for Hibernate code generator {{:java:hibernate_code_gen.png|}} 
-  - Step5: Config Main for Hibernate code generator 
-    * Check option **Reverse engineer from JDBC Connection** 
-    * And enter package name, for example: **vn.casino.entities**{{:java:hibernate-code-generator-main.jpg|}}  
-  - Step6: Config Export for **Hibernate code generator**: Check 2 options **Domain Code and Hibernate XML Mappings**: {{:java:hibernate-code-generator-exporter.jpg|}}Note: You add option **Hibernate XML Configuration** to generate **hibernate.cfg.xml** 
-  - Step7: Click **Run** to generate java code and Hibernate XML Mappings 
-==== Hibernate Foreign key Using XML mapping ==== 
-refer:  
-  * one-to-one: http://www.tutorialspoint.com/hibernate/hibernate_one_to_one_mapping.htm 
-  * one-to-many: http://www.tutorialspoint.com/hibernate/hibernate_one_to_many_mapping.htm 
-==== Hibernate log ==== 
-You need to config display log for package org.hibernate with log4j to check hibernate log<code ini> 
-# org.hibernate FileAppender 
-log4j.appender.hibernateAppender=org.apache.log4j.DailyRollingFileAppender 
-log4j.appender.hibernateAppender.layout=org.apache.log4j.PatternLayout 
-log4j.appender.hibernateAppender.File=logs/cardgameExt/hibernate.log 
-log4j.appender.hibernateAppender.layout.ConversionPattern=%d{dd MMM yyyy | HH:mm:ss,SSS} | %-5p | %t | %c{3} | %3x | %m%n 
-log4j.appender.hibernateAppender.Encoding=UTF-8 
-log4j.appender.hibernateAppender.DatePattern='.'yyyy-MM-dd 
-log4j.category.org.hibernate=INFO,consoleAppender,hibernateAppender 
 </code> </code>
 ===== Using external libraries for coding java with eclipse ===== ===== Using external libraries for coding java with eclipse =====
java/java.1658815598.txt.gz · Last modified: 2022/10/29 16:15 (external edit)