org.hibernate.connection.ConnectionProvider Java Examples

The following examples show how to use org.hibernate.connection.ConnectionProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testUserProvidedConnection() throws Exception {
	ConnectionProvider dcp = new DriverManagerConnectionProvider();
	dcp.configure( Environment.getProperties() );
	Session s = getSessions().openSession( dcp.getConnection() );
	Transaction tx = s.beginTransaction();
	s.find("from Fo");
	tx.commit();
	Connection c = s.disconnect();
	assertTrue( c!=null );
	s.reconnect(c);
	tx = s.beginTransaction();
	s.find("from Fo");
	tx.commit();
	assertTrue( s.close()==c );
	c.close();
}
 
Example #2
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the DataSource of the given SessionFactory.
 * @param sessionFactory the SessionFactory to check
 * @return the DataSource, or {@code null} if none found
 * @see org.hibernate.engine.SessionFactoryImplementor#getConnectionProvider
 * @see LocalDataSourceConnectionProvider
 */
public static DataSource getDataSource(SessionFactory sessionFactory) {
	if (sessionFactory instanceof SessionFactoryImplementor) {
		ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider();
		if (cp instanceof LocalDataSourceConnectionProvider) {
			return ((LocalDataSourceConnectionProvider) cp).getDataSource();
		}
	}
	return null;
}
 
Example #3
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ConnectionProvider getConnectionProvider() {
	return settings.getConnectionProvider();
}
 
Example #4
Source File: Hibernate3Access.java    From snakerflow with Apache License 2.0 5 votes vote down vote up
protected Connection getConnection() throws SQLException {
    if (sessionFactory instanceof SessionFactoryImplementor) {
        ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider();
        return cp.getConnection();
    }
    return super.getConnection();
}
 
Example #5
Source File: SessionFactoryUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determine the DataSource of the given SessionFactory.
 * @param sessionFactory the SessionFactory to check
 * @return the DataSource, or {@code null} if none found
 * @see org.hibernate.engine.SessionFactoryImplementor#getConnectionProvider
 * @see LocalDataSourceConnectionProvider
 */
public static DataSource getDataSource(SessionFactory sessionFactory) {
	if (sessionFactory instanceof SessionFactoryImplementor) {
		ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider();
		if (cp instanceof LocalDataSourceConnectionProvider) {
			return ((LocalDataSourceConnectionProvider) cp).getDataSource();
		}
	}
	return null;
}
 
Example #6
Source File: SettingsFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected ConnectionProvider createConnectionProvider(Properties properties) {
	return ConnectionProviderFactory.newConnectionProvider(properties);
}
 
Example #7
Source File: Settings.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ConnectionProvider getConnectionProvider() {
	return connectionProvider;
}
 
Example #8
Source File: Settings.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
void setConnectionProvider(ConnectionProvider provider) {
	connectionProvider = provider;
}
 
Example #9
Source File: SuppliedConnectionProviderConnectionHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) {
	this.provider = provider;
}
 
Example #10
Source File: NewerPerformanceTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSimultaneous() throws Exception {

		ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );

		for ( int n=2; n<4000; n*=2 ) {

			Session s = openSession();
			s.delete("from Simple");
			s.flush();
			Simple[] simples = new Simple[n];
			Serializable[] ids = new Serializable[n];
			for ( int i=0; i<n; i++ ) {
				simples[i] = new Simple();
				simples[i].init();
				simples[i].setCount(i);
				ids[i] = new Long(i);
				s.save(simples[i], ids[i]);
			}
			s.flush();
			s.connection().commit();
			s.close();

			//allow cache to settle

			s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			Connection c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			//Now do timings

			s = openSession();
			long time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h1");
			long hiber = System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j1" );
			long jdbc = System.currentTimeMillis() - time;
			cp.closeConnection(c);

			s = openSession();
			time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h2");
			hiber += System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			s = openSession();
			time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h2");
			hiber += System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			System.out.println( "Objects: " + n + " - Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );

		}

		cp.close();
		System.gc();
	}
 
Example #11
Source File: NewerPerformanceTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testJdbcOnly() throws Exception {

		ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );

		for ( int n=2; n<4000; n*=2 ) {

			Session s = openSession();
			Simple[] simples = new Simple[n];
			s.delete("from Simple");
			s.flush();
			Serializable[] ids = new Serializable[n];
			for ( int i=0; i<n; i++ ) {
				simples[i] = new Simple();
				simples[i].init();
				simples[i].setCount(i);
				ids[i] = new Long(i);
				s.save(simples[i], ids[i]);
			}
			s.flush();
			s.connection().commit();
			s.close();


			//Now do timings

			Connection c = cp.getConnection();
			long time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j1" );
			long jdbc = System.currentTimeMillis() - time;
			cp.closeConnection(c);

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );

		}

		cp.close();
		System.gc();
	}
 
Example #12
Source File: PerformanceTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testMany() throws Exception {

		ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );

		long hiber=0;
		long jdbc=0;

		for ( int n=0; n<20; n++ ) {

			Simple[] simples = new Simple[n];
			Serializable[] ids = new Serializable[n];
			for ( int i=0; i<n; i++ ) {
				simples[i] = new Simple();
				simples[i].init();
				simples[i].setCount(i);
				ids[i] = new Long(i);
			}

			//allow cache to settle

			Session s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			Connection c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			//Now do timings

			int N=30;

			long time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				s = openSession();
				hibernate(s, simples, ids, n, "h1");
				s.close();
			}
			hiber += System.currentTimeMillis() - time;

			time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				c = cp.getConnection();
				directJDBC( c, simples, ids, n, "j1" );
				cp.closeConnection(c);
			}
			jdbc += System.currentTimeMillis() - time;

			time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				s = openSession();
				hibernate(s, simples, ids, n, "h2");
				s.close();
			}
			hiber += System.currentTimeMillis() - time;

			time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				c = cp.getConnection();
				directJDBC( c, simples, ids, n, "j2" );
				cp.closeConnection(c);
			}
			jdbc += System.currentTimeMillis() - time;

			time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				s = openSession();
				hibernate(s, simples, ids, n, "h1");
				s.close();
			}
			hiber += System.currentTimeMillis() - time;

			time = System.currentTimeMillis();
			for (int i=0; i<N; i++) {
				c = cp.getConnection();
				directJDBC( c, simples, ids, n, "j1" );
				cp.closeConnection(c);
			}
			jdbc += System.currentTimeMillis() - time;

		}

		System.out.println( "Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );

		cp.close();
		System.gc();
	}
 
Example #13
Source File: PerformanceTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSimultaneous() throws Exception {

		ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );

		for ( int n=2; n<4000; n*=2 ) {

			Simple[] simples = new Simple[n];
			Serializable[] ids = new Serializable[n];
			for ( int i=0; i<n; i++ ) {
				simples[i] = new Simple();
				simples[i].init();
				simples[i].setCount(i);
				ids[i] = new Long(i);
			}

			//allow cache to settle

			Session s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			Connection c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			s = openSession();
			hibernate(s, simples, ids, n, "h0");
			s.close();

			c = cp.getConnection();
			directJDBC( c, simples, ids, n, "j0" );
			cp.closeConnection(c);

			//Now do timings

			s = openSession();
			long time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h1");
			long hiber = System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j1" );
			long jdbc = System.currentTimeMillis() - time;
			cp.closeConnection(c);

			s = openSession();
			time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h2");
			hiber += System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			s = openSession();
			time = System.currentTimeMillis();
			hibernate(s, simples, ids, n, "h2");
			hiber += System.currentTimeMillis() - time;
			s.close();

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			System.out.println( "Objects: " + n + " - Hibernate: " + hiber + "ms / Direct JDBC: " + jdbc + "ms = Ratio: " + ( (float) hiber )/jdbc );

		}

		cp.close();
		System.gc();
	}
 
Example #14
Source File: PerformanceTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testJdbcOnly() throws Exception {

		ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider( Environment.getProperties() );

		for ( int n=2; n<4000; n*=2 ) {

			Simple[] simples = new Simple[n];
			Serializable[] ids = new Serializable[n];
			for ( int i=0; i<n; i++ ) {
				simples[i] = new Simple();
				simples[i].init();
				simples[i].setCount(i);
				ids[i] = new Long(i);
			}

			//Now do timings

			Connection c = cp.getConnection();
			long time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j1" );
			long jdbc = System.currentTimeMillis() - time;
			cp.closeConnection(c);

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			c = cp.getConnection();
			time = System.currentTimeMillis();
			directJDBC( c, simples, ids, n, "j2" );
			jdbc += System.currentTimeMillis() - time;
			cp.closeConnection(c);

			System.out.println( "Objects: " + n + " Direct JDBC: " + jdbc );

		}

		cp.close();
		System.gc();
	}
 
Example #15
Source File: SessionFactoryImplementor.java    From cacheonix-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Get the connection provider
 */
public ConnectionProvider getConnectionProvider();