/** * Tests whether Connection.changeUser() (and thus pooled connections) restore character set * information correctly. * * @throws Exception if the test fails. */ public void testChangeUserAndCharsets() throws Exception { if (versionMeetsMinimum(4, 1)) { MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setURL(BaseTestCase.dbUrl); ds.setCharacterEncoding("utf-8"); PooledConnection pooledConnection = ds.getPooledConnection(); Connection connToMySQL = pooledConnection.getConnection(); this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results"); assertTrue(this.rs.next()); String toCheck = null; if (versionMeetsMinimum(4, 1, 15)) { if (versionMeetsMinimum(5, 0)) { if (versionMeetsMinimum(5, 0, 13)) { toCheck = null; } else { toCheck = "NULL"; } } else { toCheck = null; } } else { toCheck = "NULL"; } assertEquals(toCheck, this.rs.getString(1)); this.rs = connToMySQL .createStatement() .executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); // Cause of utf8mb4 assertEquals(0, this.rs.getString(2).indexOf("utf8")); connToMySQL.close(); connToMySQL = pooledConnection.getConnection(); this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results"); assertTrue(this.rs.next()); assertEquals(toCheck, this.rs.getString(1)); this.rs = connToMySQL .createStatement() .executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); // Cause of utf8mb4 assertEquals(0, this.rs.getString(2).indexOf("utf8")); pooledConnection.getConnection().close(); } }
public static void main(String[] args) throws IOException, TasteException, SAXException, ParserConfigurationException { String docIdsTitle = "src/main/resources/docIdsTitles.xml"; long userId = 2; // Integer neighbors = Integer.parseInt(args[2]); InputSource is = new InputSource(new FileInputStream(docIdsTitle)); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); SAXParser sp = factory.newSAXParser(); WikiContentHandler handler = new WikiContentHandler(); sp.parse(is, handler); MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource(); dataSource.setDatabaseName("se"); dataSource.setUser("root"); dataSource.setPassword(""); dataSource.setCachePreparedStatements(true); dataSource.setCachePrepStmts(true); dataSource.setCacheResultSetMetadata(true); dataSource.setAlwaysSendSetIsolation(false); dataSource.setElideSetAutoCommits(true); // create the data model DataModel dataModel = new JDBCDataModelImpl(new ConnectionPoolDataSource(dataSource)); // Create an ItemSimilarity ItemSimilarity itemSimilarity = new LogLikelihoodSimilarity(dataModel); // Create an Item Based Recommender ItemBasedRecommender recommender = new GenericItemBasedRecommender(dataModel, itemSimilarity); // Get the recommendations List<RecommendedItem> recommendations = recommender.recommend(userId, 5); // TasteUtils.printRecs(recommendations, handler.map); for (RecommendedItem recommendedItem : recommendations) { System.out.println("User:"******"Movie ID : " + recommendedItem); } }
@Override public void evaluate() throws Throwable { dataSource.setServerName("localhost"); dataSource.setPassword("root"); dataSource.setUser("root"); dataSource.setDatabaseName("Bank"); try { cleanTableAccounts(); statement.evaluate(); } finally { // dataSource.getPooledConnection().close(); } }
private void cleanTransferHistory() { PreparedStatement preparedStatement = null; Connection connection = null; try { connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStatement = connection.prepareStatement("DELETE FROM TransferHistory"); preparedStatement.executeUpdate(); preparedStatement = connection.prepareStatement("ALTER TABLE TransferHistory AUTO_INCREMENT = 1"); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.setAutoCommit(true); } if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } } cleanSession(); }
private void cleanSession() { PreparedStatement preparedStatement = null; try { preparedStatement = dataSource.getConnection().prepareStatement("DELETE FROM Session"); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } } cleanUsers(); }