java:java
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:java [2022/07/26 06:06] – [Hibernate connection pooling and mysql connection timeout] admin | java:java [2024/05/31 05:22] (current) – [Random in Java] admin | ||
|---|---|---|---|
| Line 401: | Line 401: | ||
| // so add 1 to make it inclusive | // so add 1 to make it inclusive | ||
| ThreadLocalRandom.current().nextInt(min, | ThreadLocalRandom.current().nextInt(min, | ||
| + | </ | ||
| + | ==== Md5 ==== | ||
| + | <code java> | ||
| + | import java.util.HashMap; | ||
| + | import java.security.MessageDigest; | ||
| + | public static String md5(String original) { | ||
| + | try{ | ||
| + | MessageDigest md = MessageDigest.getInstance(" | ||
| + | md.update(original.getBytes()); | ||
| + | byte[] digest = md.digest(); | ||
| + | StringBuffer sb = new StringBuffer(); | ||
| + | for (byte b : digest) { | ||
| + | sb.append(String.format(" | ||
| + | } | ||
| + | return sb.toString(); | ||
| + | }catch(Exception e){ | ||
| + | return ""; | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| ==== Java Reflection ==== | ==== Java Reflection ==== | ||
| Line 562: | Line 581: | ||
| * **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 ==== |
| + | {{: | ||
| refer: | refer: | ||
| - | * https://docs.oracle.com/javase/ | + | * https://www.baeldung.com/thread-pool-java-and-guava |
| - | * http:// | + | |
| * http:// | * http:// | ||
| + | * http:// | ||
| 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 1222: | ||
| session.close(); | session.close(); | ||
| </ | </ | ||
| - | ==== Hibernate | + | |
| + | === Custom Configure === | ||
| + | refer: http:// | ||
| + | === Hibernate | ||
| + | === Hibernate reverseengineering tool === | ||
| + | refer: | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | * http:// | ||
| + | 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)** {{: | ||
| + | * database name: test | ||
| + | * username: admin | ||
| + | * password: admin12!@ | ||
| + | - Step2: **Create Hibernate Console Configuration**: | ||
| + | - Step3: view **Hibernate Console Configuration** created from Step2. Go to **window-> | ||
| + | - Step4: Create configuration for Hibernate code generator {{: | ||
| + | - Step5: Config Main for Hibernate code generator | ||
| + | * Check option **Reverse engineer from JDBC Connection** | ||
| + | * And enter package name, for example: **vn.casino.entities**{{: | ||
| + | - Step6: Config Export for **Hibernate code generator**: | ||
| + | - Step7: Click **Run** to generate java code and Hibernate XML Mappings | ||
| + | ==== Hibernate Foreign key Using XML mapping ==== | ||
| + | refer: | ||
| + | * one-to-one: http:// | ||
| + | * one-to-many: | ||
| + | ==== 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/ | ||
| + | log4j.appender.hibernateAppender.layout.ConversionPattern=%d{dd MMM yyyy | HH: | ||
| + | log4j.appender.hibernateAppender.Encoding=UTF-8 | ||
| + | log4j.appender.hibernateAppender.DatePattern=' | ||
| + | log4j.category.org.hibernate=INFO, | ||
| + | </ | ||
| + | ===== 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, | Connection pooling takes care of **managing connections(prepare, | ||
| Line 1209: | Line 1267: | ||
| * 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< | ||
| - | 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. | ||
| - | | ||
| - | Release all resources held by this provider. | ||
| - | | ||
| - | Dispose of a used connection. | ||
| - | | ||
| - | Initialize the connection provider from given properties. | ||
| - | | ||
| - | Grab a connection, with the autocommit mode specified by hibernate.connection.autocommit. | ||
| - | | ||
| - | 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. | ||
| - | </ | ||
| 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/ | + | |
| - | * https:// | + | |
| - | * {{: | + | |
| * {{: | * {{: | ||
| - | Below are basic steps config for connections in Hibernate(each connection in MySQL was a session in Hibernate): | + | * {{: |
| + | |||
| + | ==== Default Hibernate connection pooling ==== | ||
| + | Below are basic steps config for connections in Hibernate(each connection in MySQL was a session in Hibernate)(refer: https:// | ||
| - Config max connections in MySQL: | - Config max connections in MySQL: | ||
| * config temporary in MySQL Admin console:< | * config temporary in MySQL Admin console:< | ||
| Line 1266: | Line 1305: | ||
| } | } | ||
| </ | </ | ||
| - | === Default JDBC Connection and Default Connection Pool=== | ||
| Most important Hibernate JDBC properties:< | Most important Hibernate JDBC properties:< | ||
| < | < | ||
| Line 1275: | Line 1313: | ||
| < | < | ||
| </ | </ | ||
| - | === org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool === | + | ==== org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool ==== |
| refer: | refer: | ||
| * https:// | * https:// | ||
| * Configuration c3p0: http:// | * Configuration c3p0: http:// | ||
| + | * org.hibernate.connection Interface ConnectionProvider< | ||
| + | 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. | ||
| + | | ||
| + | Release all resources held by this provider. | ||
| + | | ||
| + | Dispose of a used connection. | ||
| + | | ||
| + | Initialize the connection provider from given properties. | ||
| + | | ||
| + | Grab a connection, with the autocommit mode specified by hibernate.connection.autocommit. | ||
| + | | ||
| + | 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. | ||
| + | </ | ||
| * org.hibernate.connection.C3P0ConnectionProvider https:// | * org.hibernate.connection.C3P0ConnectionProvider https:// | ||
| import javax.sql.DataSource; | import javax.sql.DataSource; | ||
| Line 1325: | Line 1382: | ||
| * Global statements pool(statements cached): <code xml> | * Global statements pool(statements cached): <code xml> | ||
| < | < | ||
| - | </ | ||
| - | === Custom Configure === | ||
| - | refer: http:// | ||
| - | === Hibernate map editor === | ||
| - | === Hibernate reverseengineering tool === | ||
| - | refer: | ||
| - | * http:// | ||
| - | * http:// | ||
| - | * http:// | ||
| - | * http:// | ||
| - | 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)** {{: | ||
| - | * database name: test | ||
| - | * username: admin | ||
| - | * password: admin12!@ | ||
| - | - Step2: **Create Hibernate Console Configuration**: | ||
| - | - Step3: view **Hibernate Console Configuration** created from Step2. Go to **window-> | ||
| - | - Step4: Create configuration for Hibernate code generator {{: | ||
| - | - Step5: Config Main for Hibernate code generator | ||
| - | * Check option **Reverse engineer from JDBC Connection** | ||
| - | * And enter package name, for example: **vn.casino.entities**{{: | ||
| - | - Step6: Config Export for **Hibernate code generator**: | ||
| - | - Step7: Click **Run** to generate java code and Hibernate XML Mappings | ||
| - | ==== Hibernate Foreign key Using XML mapping ==== | ||
| - | refer: | ||
| - | * one-to-one: http:// | ||
| - | * one-to-many: | ||
| - | ==== 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/ | ||
| - | log4j.appender.hibernateAppender.layout.ConversionPattern=%d{dd MMM yyyy | HH: | ||
| - | log4j.appender.hibernateAppender.Encoding=UTF-8 | ||
| - | log4j.appender.hibernateAppender.DatePattern=' | ||
| - | log4j.category.org.hibernate=INFO, | ||
| </ | </ | ||
| ===== Using external libraries for coding java with eclipse ===== | ===== Using external libraries for coding java with eclipse ===== | ||
java/java.1658815598.txt.gz · Last modified: (external edit)
