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:51] 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 1239: Line 1240:
 log4j.appender.hibernateAppender.DatePattern='.'yyyy-MM-dd log4j.appender.hibernateAppender.DatePattern='.'yyyy-MM-dd
 log4j.category.org.hibernate=INFO,consoleAppender,hibernateAppender 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 takes care of **managing connections(prepare,open and close)**
 +  * Connection Pool API is responsible for opening N connections and leave them ready for our application.
 +  * Our application will just ask for the pool to get a new connection, it will use it and then deliver it back to the Pool.
 +  * 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
 +refer: 
 +  * https://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html
 +  * {{:java:connection-pool.png|}}
 +  * {{: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 temporary in MySQL Admin console:<code sql>
 +SET GLOBAL MAX_CONNECTIONS = 200;
 +</code>
 +    * config in **my.cnf or my.ini** of MySQL:<code ini>
 +MAX_CONNECTIONS = 200
 +</code>
 +  - Config max pool connections:<code>
 +<property name="hibernate.connection.pool_size">100</property>
 +</code>
 +  - coding to **close session** after call(in **doWork()** function):<code java>
 +public class DBConnection {
 +    private static SessionFactory factory;
 +    static {
 +        factory = new Configuration().configure().buildSessionFactory();
 +    }
 +    public Session getSession() {
 +        return factory.openSession();
 +    }
 +
 +    public void doWork() {
 +        Session session = getSession();
 +        // do work.
 +        session.close();
 +    }
 +    // Call this during shutdown
 +    public static void close() {
 +        factory.close();
 +    }
 +}
 +</code>
 +Most important Hibernate JDBC properties:<code xml>
 +<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 +<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 +<property name="hibernate.connection.username">root</property>
 +<property name="hibernate.connection.password">secret</property>
 +<property name="hibernate.connection.url">jdbc:mysql:///cardgame?useUnicode=true;characterEncoding=utf-8</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.
 +==== org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool ====
 +refer:
 +  * https://www.mchange.com/projects/c3p0/ and github https://github.com/swaldman/c3p0
 +  * 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>
 +import javax.sql.DataSource;
 +import com.mchange.v2.c3p0.DataSources;
 +.....
 +public class C3P0ConnectionProvider
 + implements ConnectionProvider, Configurable, Stoppable, ServiceRegistryAwareService {
 +.....
 + private DataSource ds;
 + @Override
 + public Connection getConnection() throws SQLException {
 + final Connection c = ds.getConnection();
 +.....
 + @Override
 + public void configure(Map<String, Object> props) {
 +.....
 + final DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl, connectionProps );
 + ds = DataSources.pooledDataSource( unpooled, allProps );
 +.....
 + @Override
 + public void stop() {
 + try {
 + DataSources.destroy( ds );
 + }
 + catch (SQLException sqle) {
 + C3P0_MSG_LOGGER.unableToDestroyC3p0ConnectionPool( sqle );
 + }
 + }
 +
 +</code> With Datasources from c3p0 source https://github.com/swaldman/c3p0/blob/master/src/java/com/mchange/v2/c3p0/DataSources.java
 +Download jar for using hibernate c3p0:
 +  * Step1: Download [[http://www.java2s.com/Code/Jar/h/Downloadhibernatec3p0422finaljar.htm|hibernate-c3p0-4.2.2.final.jar]]
 +  * Step2: Download [[https://sourceforge.net/projects/c3p0/files/c3p0-bin/c3p0-0.9.5.2/c3p0-0.9.5.2.bin.zip/download | c3p0-0.9.5.2.jar & mchange-commons-java-0.2.11.jar]]
 +Hibernate uses its org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider for connection pooling if you **set the hibernate.c3p0.* properties**. properties.<code xml>
 +<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
 +<property name="hibernate.c3p0.min_size">5</property>
 +<property name="hibernate.c3p0.max_size">20</property>
 +<property name="hibernate.c3p0.timeout">2000</property>
 +<property name="hibernate.c3p0.max_statements">1000</property>
 +</code> => You need to turn on [[java:java#hibernate_log|hibernate log]] to check debug log and error log when using c3p0 pool
 +Some basic pool configuration in hibernate-c3p0:
 +  * connnection pool: <code xml>
 +<property name="hibernate.c3p0.min_size">5</property>
 +<property name="hibernate.c3p0.max_size">20</property>
 +</code>
 +  * Global statements pool(statements cached): <code xml>
 +<property name="hibernate.c3p0.max_statements">1000</property>
 </code> </code>
 ===== Using external libraries for coding java with eclipse ===== ===== Using external libraries for coding java with eclipse =====
java/java.1658818301.txt.gz · Last modified: 2022/10/29 16:15 (external edit)